テキストエディタ「mi」v3.0のドキュメントに記述してあるAppleScriptをコンパイル(構文確認)して元の書類に書き戻し、結果を新規Windowに表示するAppleScriptです。
とくに、miでなくても他のテキストエディタでも同様の動作は可能です。
AppleScript名:miでAppleScriptを実行して結果を新規Windowで返す |
— Created 2018-04-24 by Takaaki Naganoya — 2018 Piyomaru Software use AppleScript version "2.5" use scripting additions use framework "Foundation" use framework "OSAKit" property NSThread : a reference to current application’s NSThread property OSAScript : a reference to current application’s OSAScript property NSTextView : a reference to current application’s NSTextView property OSALanguage : a reference to current application’s OSALanguage property OSAScriptView : a reference to current application’s OSAScriptView property OSAScriptController : a reference to current application’s OSAScriptController property OSALanguageInstance : a reference to current application’s OSALanguageInstance property theResult : "" –result set my theResult to "" tell application "mi" tell front document if mode is not equal to "AppleScript" then return set theSource to content end tell end tell set asSource to my compileASandReturnString:theSource my performSelectorOnMainThread:"execASandReturnString:" withObject:theSource waitUntilDone:true tell application "mi" tell front document set content to asSource end tell set newDoc to make new document tell newDoc set mode to "AppleScript" set content to theResult end tell end tell on execASandReturnString:(theSource as string) set targX to 1024 –View Width set targY to 2048 –View Height set osaCon to current application’s OSAScriptController’s alloc()’s init() set osaView to current application’s OSAScriptView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, targX, targY)) set resView to NSTextView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, targX, targY)) resView’s setRichText:true resView’s useAllLigatures:true osaCon’s setScriptView:osaView osaCon’s setLanguage:(OSALanguage’s languageForName:"AppleScript") osaCon’s setResultView:resView osaView’s setString:theSource osaCon’s compileScript:(missing value) –Compile(構文確認) osaCon’s runScript:(missing value) set asRes to resView’s |string|() as list of string or string –as anything set aRes to (osaView’s |string|()) as string copy asRes to theResult end execASandReturnString: on compileASandReturnString:(theSource as string) set targX to 1024 –View Width set targY to 2048 –View Height set osaCon to current application’s OSAScriptController’s alloc()’s init() set osaView to current application’s OSAScriptView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, targX, targY)) set resView to NSTextView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, targX, targY)) resView’s setRichText:true resView’s useAllLigatures:true osaCon’s setScriptView:osaView osaCon’s setLanguage:(OSALanguage’s languageForName:"AppleScript") osaCon’s setResultView:resView osaView’s setString:theSource osaCon’s compileScript:(missing value) –Compile(構文確認) set aRes to (osaView’s |string|()) as string return aRes end compileASandReturnString: |