AppleScript名:迷路をRTFで作成して脱出経路を赤く着色する v3 |
— Created 2017-09-19 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "AppKit" use framework "MazeFinder" –https://github.com/tcjennings/MazeFinder property NSFont : a reference to current application’s NSFont property NSUUID : a reference to current application’s NSUUID property NSColor : a reference to current application’s NSColor property NSString : a reference to current application’s NSString property Board2D : a reference to current application’s Board2D property Pathfinder : a reference to current application’s Pathfinder property NSDictionary : a reference to current application’s NSDictionary property NSLiteralSearch : a reference to current application’s NSLiteralSearch property NSMutableArray : a reference to current application’s NSMutableArray property NSFontAttributeName : a reference to current application’s NSFontAttributeName property NSMutableAttributedString : a reference to current application’s NSMutableAttributedString property NSForegroundColorAttributeName : a reference to current application’s NSForegroundColorAttributeName property NSDocumentTypeDocumentAttribute : a reference to current application’s NSDocumentTypeDocumentAttribute –NSNotFoundはプロパティに代入しても認識されなかった set targFontName to "Courier-Bold" –PostScript Name set targFontSize to 13 –Point –迷路テキストデータ作成→Attributed Stringに set aStr to create2DMazeAndSolveIt(30, 30, 1, 1, 28, 28) of me –Plain Text set anAttr to changeAttrStrsFontAttribute(aStr, targFontName, targFontSize) of me –Attributed String –迷路データに着色(参照呼び出し, Call by reference) markCharOfAttributedString(anAttr, aStr, "!", (NSColor’s redColor())) of me –結果のRTFをデスクトップ上に書き出す。ファイル名はUUID.rtf set targFol to current application’s NSHomeDirectory()’s stringByAppendingPathComponent:"Desktop" set aRes to my saveStyledTextAsRTF(targFol, anAttr) –指定のAttributed String内で指定文字列が含まれる箇所に指定の色をつける(結果はメイン側に参照渡し) on markCharOfAttributedString(anAttr, origStr, aTargStr, aColor) set rList to searchWordWithRange(origStr, aTargStr) of me repeat with ii in rList (anAttr’s addAttribute:(NSForegroundColorAttributeName) value:aColor range:ii) end repeat end markCharOfAttributedString –指定の文字列をAttributed Stringに変換して任意のフォントを一括指定 on changeAttrStrsFontAttribute(aStr, aFontPSName, aFontSize) set tmpAttr to NSMutableAttributedString’s alloc()’s initWithString:aStr set aRange to current application’s NSMakeRange(0, tmpAttr’s |length|()) set aVal1 to NSFont’s fontWithName:aFontPSName |size|:aFontSize tmpAttr’s beginEditing() tmpAttr’s addAttribute:(NSFontAttributeName) value:aVal1 range:aRange tmpAttr’s endEditing() return tmpAttr end changeAttrStrsFontAttribute –指定テキストデータ(atargText)内に、指定文字列(aSearchStr)が含まれる範囲情報(NSRange)をすべて取得する on searchWordWithRange(aTargText, aSearchStr) set aStr to NSString’s stringWithString:aTargText set bStr to NSString’s stringWithString:aSearchStr set hitArray to NSMutableArray’s alloc()’s init() set cNum to (aStr’s |length|()) as integer set aRange to current application’s NSMakeRange(0, cNum) repeat set detectedRange to aStr’s rangeOfString:bStr options:NSLiteralSearch range:aRange set aLoc to (detectedRange’s location) –CAUTION !!!! Sometimes aLoc returns not NSNotFound (-1) but a Very large number if (aLoc > 9.999999999E+9) or (aLoc = (current application’s NSNotFound)) then exit repeat hitArray’s addObject:detectedRange set aNum to aLoc as integer set bNum to (detectedRange’s |length|) as integer set aRange to current application’s NSMakeRange(aNum + bNum, cNum – (aNum + bNum)) end repeat return hitArray end searchWordWithRange –スタイル付きテキストを指定フォルダ(POSIX path)にRTFで書き出し on saveStyledTextAsRTF(targFol, aStyledString) set bstyledLength to aStyledString’s |string|()’s |length|() set bDict to NSDictionary’s dictionaryWithObject:"NSRTFTextDocumentType" forKey:(NSDocumentTypeDocumentAttribute) set bRTF to aStyledString’s RTFFromRange:(current application’s NSMakeRange(0, bstyledLength)) documentAttributes:bDict set theName to (NSUUID’s UUID()’s UUIDString()) set thePath to NSString’s stringWithString:targFol set thePath to (thePath’s stringByAppendingPathComponent:theName)’s stringByAppendingPathExtension:"rtf" return (bRTF’s writeToFile:thePath atomically:true) as boolean end saveStyledTextAsRTF –2D迷路を作成して脱出経路を検索して文字列で迷路データを出力する –迷路サイズ x,y スタート座標 x, y ゴール座標 x,y on create2DMazeAndSolveIt(xMax, yMax, xStart, yStart, xGoal, yGoal) set myBoard to Board2D’s alloc()’s init() myBoard’s setupBoardWithRows:xMax WithColumns:yMax –60×60ぐらいが上限。正方形でないとダメ myBoard’s createMazeFromBoard() set myPathfinder to Pathfinder’s alloc()’s init() set myPath to myPathfinder’s findPathThroughMaze:myBoard fromX:xStart fromY:yStart toX:xGoal toY:yGoal set aCount to myPath’s |count|() if aCount > 0 then set aRes to myBoard’s drawPathThroughMaze:myPath return aRes as string else return false end if end create2DMazeAndSolveIt |
More from my site
(Visited 85 times, 1 visits today)
値渡しハンドラと参照渡しハンドラ – AppleScriptの穴 says:
[…] ・迷路をRTFで作成して脱出経路を赤く着色する v3 ・画像+文字作成テスト_v4 ・Tag CloudっぽいRTFの作成 […]