Archive for the 'ASDictionary' Category

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

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

05/02 AS辞書ぜんぶHTMLに書き出し v3

AS Dictionaryで、Apple純正のデフォルトインストールされているアプリケーションの用語辞書をHTML書き出しするAppleScriptです。

Mac OS Xの新バージョンなど、内容や動作確認を行っておく必要がある場合に、インストール直後のOSに対してデフォルトでインストールされているアプリケーションやOSAXのAppleScript用語辞書を一括でHTML書き出ししておきます。

書き出したHTML書類は、FileMergeを使ってdiff表示を行い、機能の変更点を割り出すのに使います。とくにβ段階のOSでは個別の機能変更について詳細なガイダンスが出ているわけではないため、こうしたツールで変更点を推測することが重要です(こうした方法論が確立できていなかった頃は、OSのバージョンアップのたびにひどい目に遭っていました)。

Apple純正アプリ/OSAXの辞書書き出し……といっていますが、書き出し対象としてとくにApple純正アプリに限っているわけではないため、すでに実運用段階で(Apple純正以外の)アプリが大量に入っている環境においては、何かの対策を講じておく必要がありそうです。

スクリプト名:AS辞書ぜんぶHTMLに書き出し v3

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書き出し
    
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のリネーム
    
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

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

06/02 AS Dictionaryで指定アプリケーションのファイルのAS辞書をHTMLに書き出す

アプリケーションのバージョンが単体で上がったり、OSのバージョンのついでにアプリケーションのバージョンが上がったときに、AppleScriptの用語辞書が変更されることがあります。とくに、OSのメジャーバージョンが変わった時には、AppleScript用語辞書の変更を確認するだけでてんやわんやです。

そのうえ……Beta版とRelease版で挙動が変わってしまったような場合には、非常に困るわけですが…………………それはさておき、Appleが好き勝手に行う辞書内容の変更から身を守ることができないとしても、その変更を容易に検出できるようにしておくことは重要です。

そこで登場するのが、アプリケーションのAppleScript用語辞書をHTMLに書き出す「ASDictionary」。数年前から愛用しているツールですが、国内ではあまりご存じの方はいないようで……さらにASDictionaryが最近AppleScriptからコントロールできるようになり、複数のアプリケーションからまとめて用語辞書を書き出すといったAppleScriptらしい自動処理ができるようになってきました。

diff2.jpg

HTMLに書き出せれば、単なるテキストなので(sdefファイルの状態でもいいような気もしますが)diff系のツールで差分検出が気軽に行えます。とくに、Xcode Toolsに標準添付されているApple謹製diffツール「FileMerge」の使い勝手は最高で、いにしえのCodeWarriorのdiffを上回るとかパクっただけじゃないかとか……諸説ありますが、まあとにかく便利なわけです。

diff3.jpg

本AppleScriptは、指定のアプリケーションからAppleScript用語辞書をHTMLで書き出して、アプリケーションのバージョン番号をHTMLファイルに反映させます。

OSのアップデートがあるたびにAppleScript用語辞書を書き出し、FileMergeで差分をとっては辞書内容に変更がないかを確認している今日このごろです。ちなみに、本日アップデートされたiTunes 8.2とQuickTime Player 7.6.2には辞書内容の変更はありません。

diff4.jpg

diff.jpg

スクリプト名:AS Dictionaryで指定アプリケーションのファイルのAS辞書をHTMLに書き出す
set a to choose file with prompt 辞書をHTML書き出しするアプリケーションを選択

tell application Finder
  set aInfo to info for a
  
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 outPath to choose folder with prompt AS辞書HTML書き出し先フォルダを選択
set outPathStr to (outPath as string) & prodName & & sVerStr & .html

tell application ASDictionary
  set exRes to export a 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失敗時
    
display dialog HTML書き出しが失敗しました with title エラー buttons {”OK“} default button 1
    
return
  end if
  
  
Export成功時
  
set outFile to destination of (first item of exRes)
end tell

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

任意のデータから特定の文字列を置換
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

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に