Finder上で選択中のAppleScript書類の行数や種別を取得して表UIで表示するAppleScriptです。
Myriad Tables Libを用いて結果を表UIで表示しています。
当初はAppleScript書類からソースを取得するのに/usr/bin/osadecompileコマンドを使っていたのですが、OSAKit経由で処理したほうが高速な印象があります。
–> Download Executable archive for macOS 10.14 (include Myriad Tables Lib)
macOS 10.14上で動作させるためには、Script Debugger上で本Scriptをオープンするか、本Scriptをバンドル形式で保存し、バンドル内(/Contents/Resources/Script Libraries フォルダ)にMyriad Tables Libを入れ、AppleScriptアプレットとして書き出す必要があります。
また、初回実行時には「セキュリティ」の承認ダイアログが表示されるため、これをOKする必要があります。AppleScriptアプレットはFinder上でアイコンをダブルクリックすると「Finder上で選択中のアイテム」を拾えないため(その瞬間、Finder上での選択アイテムはアプレット自身になってしまうため)、DockやmacOS標準装備のスクリプトメニューに登録して起動する必要があります。
AppleScript名:Finder上で選択中のAppleScriptの行数をカウントする v2.scpt |
— Created 2019-02-04 by Takaaki Naganoya — 2019 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "OSAKit" use script "Myriad Tables Lib" version "1.0.8" –https://www.macosxautomation.com/applescript/apps/Script_Libs.html#MyriadTablesLib property |NSURL| : a reference to current application’s |NSURL| property NSArray : a reference to current application’s NSArray property NSString : a reference to current application’s NSString property OSAScript : a reference to current application’s OSAScript property NSPredicate : a reference to current application’s NSPredicate property NSURLIsPackageKey : a reference to current application’s NSURLIsPackageKey property NSURLIsDirectoryKey : a reference to current application’s NSURLIsDirectoryKey property NSURLTypeIdentifierKey : a reference to current application’s NSURLTypeIdentifierKey –Finder上で選択中のファイルのうちAppleScript書類のみ抽出 tell application "Finder" set inFiles to selection as alias list if inFiles = {} then return end tell set filRes1 to filterAliasListByUTIList(inFiles, {"com.apple.applescript.script", "com.apple.applescript.script-bundle"}) of me if filRes1 = {} then return –AppleScript書類の種別判定および行数カウント set outList to {} repeat with i in filRes1 set aName to (NSString’s stringWithString:i)’s lastPathComponent() as string set sInfo to detectScriptIsPureASorASOC(i) of me set sText to scriptSource of getASsourceFor(i) of me set sNum to count every paragraph of sText set sUTI to retUTIfromPath(i) of me if sUTI = "com.apple.applescript.script" then set sKind to "Script" else if sUTI = "com.apple.applescript.script-bundle" then set sKind to "Script Bundle" else set sKind to "Other" end if set the end of outList to {aName, sInfo, sKind, sNum} end repeat –結果を表UIで表示する tell script "Myriad Tables Lib" set aDispBounds to my makeInitialBounds:1200 withHeight:500 set theTable to make new table with data outList column headings {"script name", "ASOC", "Script Kind", "lines"} with title "AppleScript Line Count" with prompt "Your Selected Scripts" with row numbering and empty selection allowed –and can add and delete modify table theTable initial position aDispBounds column widths pattern {1, 2, 3, 4} with hidden cancel button modify columns in table theTable user date format {user format full, user format full} entry alignment align left (display table theTable) end tell –Alias listから指定UTI Listに含まれるものをPOSIX pathのリストで返す on filterAliasListByUTIList(aList as list, targUTIList as list) set outList to {} repeat with i in targUTIList set j to contents of i set aRes to filterAliasListByUTI(aList, j) of me if aRes is not equal to {} then set outList to outList & aRes end if end repeat return outList end filterAliasListByUTIList –Alias listから指定UTIに含まれるものをPOSIX pathのリストで返す on filterAliasListByUTI(aList as list, targUTI) set newList to {} repeat with i in aList set j to POSIX path of i set tmpUTI to my retUTIfromPath(j) set utiRes to my filterUTIList({tmpUTI}, targUTI) if utiRes is not equal to {} then set the end of newList to j end if end repeat return newList end filterAliasListByUTI –指定のPOSIX pathのファイルのUTIを求める on retUTIfromPath(aPOSIXPath) set aURL to |NSURL|’s fileURLWithPath:aPOSIXPath set {theResult, theValue} to aURL’s getResourceValue:(specifier) forKey:NSURLTypeIdentifierKey |error|:(missing value) if theResult = true then return theValue as string else return theResult end if end retUTIfromPath –UTIリストが指定UTIに含まれているかどうか演算を行う on filterUTIList(aUTIList, aUTIstr) set anArray to NSArray’s arrayWithArray:aUTIList set aPred to NSPredicate’s predicateWithFormat_("SELF UTI-CONFORMS-TO %@", aUTIstr) set bRes to (anArray’s filteredArrayUsingPredicate:aPred) as list return bRes end filterUTIList on makeInitialBounds:(aTWidth as integer) withHeight:(aTHeight as integer) set aDispBounds to current application’s NSScreen’s mainScreen()’s frame() if class of item 1 of aDispBounds = record then –macOS 10.10/10.11/10.12 set aWidth to (width of |size| of aDispBounds) set aHeight to (height of |size| of aDispBounds) else –macOS 10.13 or later? set aWidth to (item 1 of item 2 of aDispBounds) set aHeight to (item 2 of item 2 of aDispBounds) end if set xPos to (aWidth div 2) – (aTWidth div 2) set yPos to (aHeight div 2) – (aTHeight div 2) return {xPos, yPos, aTWidth, aTHeight} end makeInitialBounds:withHeight: –指定AppleScriptファイルがPure ASかASOCかを判定して返す on detectScriptIsPureASorASOC(aFile) set sRes to getASsourceFor(aFile) of me set sName to scriptKind of sRes –Name set sText to scriptSource of sRes –Source if sText = "" or sText = missing value then return missing value if sName = "AppleScript" then if sText contains "use framework \"Foundation\"" then return true –ASOC else return false –Pure AppleScript end if else –JXAなど他のOSA言語の場合 return sName end if end detectScriptIsPureASorASOC –指定AppleScriptファイルのソースコードを取得する(実行専用Scriptからは取得できない) on getASsourceFor(aPOSIXPath) set aURL to |NSURL|’s fileURLWithPath:(aPOSIXPath) set theScript to current application’s OSAScript’s alloc()’s initWithContentsOfURL:aURL |error|:(missing value) set scriptName to theScript’s |language|()’s |name|() as string set theSource to theScript’s source() as text return {scriptKind:scriptName, scriptSource:theSource} end getASsourceFor |
More from my site
(Visited 280 times, 1 visits today)