03/24 インストールされているアプリのAS辞書を書き出すv2
インストールされているアプリのAppleScript用語辞書をHTMLに一括書き出しするAppleScriptです。
実行したMac上にインストールされている/Applicationsフォルダ、ユーティリティーフォルダ、/Library/CoreServicesフォルダ、/Library/ScriptingAdditionsフォルダなどのアプリケーションのうち、Scriptableなもの(通常のAppleScriptからコントロール可能なもの。GUI Scriptingを経由ではなく)をピックアップして、AppleScript用語辞書をHTML書き出し、それを指定フォルダに出力します。
# 最新のXcodeは/Applicationsフォルダに入るので、一部の処理は無駄になっています
本バージョンでは、AppleScript用語辞書のHTML書き出しに伴い、プロセスが起動されてメモリーが圧迫されるのを防ぐために、対象アプリケーションのプロセスが起動したら、つぶさにkillコマンドで強制終了させています。ただし、killするのはvisible process(Dockにアイコンが表示されるプログラム)だけであり、Help Viewerのような「Dockには表示されないが、表示ウィンドウを持つ」ものはkillできていません。
また、実際に使ってみたところ……光学ドライブが内蔵されていないMacBook Air上でDVD PlayerのAppleScript用語辞書をHTML書き出ししようとして、DVDドライブが装備されていない旨エラーのダイアログが表示され……肝心のAppleScript用語辞書は書き出されていませんでした。実行時には外付けのDVDドライブでもつないでおくとよいでしょう。
OSのアップデートや、β版のOSの新版がリリースされたときに、AppleはAppleScript関連の用語辞書の修正などはアナウンスしないため、ユーザー側でマイナーバージョンごとに用語辞書をHTML書き出ししておき、前バージョンとの差分をFileMergeなどのdiffツールで比較。どこに修正が加わったかをチェックする必要があります。
ただし、このようなツールで分るのはAppleが用語辞書に対して加えた修正点だけであり、構文レベルでバグが潜んでいるとか(Mac OS X 10.3のときには「is in」演算子がバグっていて死ぬほどひどい目に遭わされました、、)、アプリケーションやOSそのものにバグがある場合には検出できません。
本気で、Appleが作るバグを検出するために数百とか数千本のAppleScriptを各種言語(英語、仏語、独語、日本語、中国語、韓国語など)環境でチェックを行うシステムを作ることを考えないではないですが、それはApple自身がとっくの昔にやっておくべき話であって、ユーザーがやる種類のものではないような気が…………。
| スクリプト名:インストールされているアプリのAS辞書を書き出すv2 |
set outPath to choose folder with prompt “AS辞書HTML書き出し先フォルダを選択” tell application “ASDictionary” to launch set apList to getScriptableAppPathList() of me set ngList to {} –処理実行時にエラーになったアプリケーション/OSAXのパスが入る –メインループ repeat with i in apList set j to contents of i if j is not equal to {} or j is not equal to “” then –アプリケーションファイルの情報を取得 tell application “Finder” set aInfo to info for j set sVer to short version of aInfo set sVer2 to prepareShortVersionStringNum(sVer) of me set sVerStr to replaceText(sVer2 as string, “.”, “”) of me set prodName to displayed name of aInfo end tell set outPathStr to (outPath as string) & prodName & ” “ & sVerStr & “.html” –HTML書き出し前のプロセス一覧を取得 tell application “System Events” set beforeList to (unix id of every process whose visible is true) log beforeList end tell –HTML書き出し(実行すると、当該のアプリケーションが起動するケース多し) set htmlRes to false tell application “ASDictionary” set exRes to export j to outPath using file formats {single file HTML} using styles {AppleScript} with showing hidden items without compacting classes if success of (first item of exRes) = false then –Export失敗時 set the end of ngList to j else –Export成功時 set htmlRes to true set outFile to destination of (first item of exRes) end if end tell –HTML書き出し後のプロセス一覧を取得(書き出し時に起動されたアプリケーションが増えている) tell application “System Events” set afterList to (unix id of every process whose visible is true and name of it is not equal to “AppleScript Editor”) –set afterList to (unix id of every process whose visible is true) log afterList end tell –プロセスIDの差分を取得する set dRes to getListDiff(beforeList, afterList) of me if dRes is not equal to {} then repeat with tmpUNIXID in dRes set anUNIXID to (contents of tmpUNIXID) as string set killRes to killProcessByID(anUNIXID) of me log killRes end repeat end if –書き出したHTMLのリネーム if htmlRes = true then tell application “Finder” tell file outFile set aName to name –書き出したHTMLファイルにバージョン番号を反映させる set bName to replaceText(aName, “-AS.html”, (” “ & sVerStr & “.html”)) of me set name to bName end tell end tell end if end if end repeat ngList –書き出せなかったアプリの一覧 –デフォルトでインストールされているAS対応アプリケーションのリストを取得する on getScriptableAppPathList() set apFol to path to applications folder set utilFol to path to utilities folder set sysFol to ((path to system folder) as string) & “Library:CoreServices:” set sysLibFol to ((path to system folder) as string) & “Library:ScriptingAdditions:” set devFol to ((path to startup disk) as string) & “Developer:Applications:” set libPath to (path to library folder) & “ScriptingAdditins:” tell application “Finder” –一般アプリケーション tell folder apFol set ap1List to (every application file whose accepts high level events is true and has scripting terminology of it is true) as alias list end tell –ユーティリティーフォルダー tell folder utilFol set ap2List to (every application file whose accepts high level events is true and has scripting terminology of it is true) as alias list end tell –SystemのCoreServicesフォルダ tell folder sysFol set ap3List to (every application file whose has scripting terminology is true) as alias list end tell –Developerフォルダ try tell folder devFol set ap4List to (every application file whose has scripting terminology is true) as alias list end tell on error set ap4List to {} end try –/System/Library/ScriptingAdditions フォルダのOSAX try tell folder sysLibFol set osax1List to (every file whose name ends with “.osax”) as alias list end tell on error set osax1List to {} end try –/Library/ScriptingAdditions フォルダのOSAX try tell folder libPath set osax2List to (every file whose name ends with “.osax”) as alias list end tell on error set osax2List to {} end try set appList to ap1List & ap2List & ap3List & ap4List & osax1List & osax2List end tell return appList end getScriptableAppPathList –任意のデータから特定の文字列を置換 on replaceText(origData, origText, repText) set curDelim to AppleScript’s text item delimiters set AppleScript’s text item delimiters to {origText} set origData to text items of origData set AppleScript’s text item delimiters to {repText} set origData to origData as text set AppleScript’s text item delimiters to curDelim –set b to origData as text return origData end replaceText –short version文字列をよろしく処理する(major.minor1.minor2) –short versionに英語や日本語の文字をズラズラ並べていた場合には所期の動作を行えない on prepareShortVersionStringNum(sVer) set curDelim to AppleScript’s text item delimiters set AppleScript’s text item delimiters to {“.”} set sList to text items of sVer set AppleScript’s text item delimiters to curDelim set sLen to length of sList if sLen = 2 then set sVer to sVer & “.0″ else if sLen = 1 then set sVer to sVer & “.0.0″ end if return sVer end prepareShortVersionStringNum –リスト間のdiffを取る –aList: before List, bList: after List on getListDiff(aList, bList) set diffList to {} repeat with i in bList set j to contents of i if j is not in aList then set the end of diffList to j end if end repeat return diffList end getListDiff –指定プロセスをしつこくkillする on killProcessByID(anID) set anID to anID as string set errorCount to 20 –10 seconds to wait repeat tell application “System Events” set dID to every process whose unix id is equal to (anID as number) end tell if dID = {} then return true do shell script “kill -9 “ & anID set errorCount to errorCount - 1 if errorCount = 0 then return false delay 0.5 end repeat end killProcessByID |




