アプリケーションのSDEF(AppleScript用語辞書)を解析して情報を取り出すシリーズの本命。指定コマンドの属性値を取り出すテスト用のAppleScriptです。
–> Download script with library
まだ、明確な成果が出ているわけではありませんが、こうしてアクセスして問題がないか、多くのアプリケーションの各コマンドで問題がないかを調査しているところです。
AppleScript名:Bundle IDで指定したアプリケーションのSDEFからコマンドを抽出テスト(指定コマンドのコマンド属性取り出し).scptd |
— – Created by: Takaaki Naganoya – Created on: 2022/08/2 — – Copyright © 2022 Piyomaru Software, All Rights Reserved — use AppleScript version "2.4" — Yosemite (10.10) or later use framework "Foundation" use framework "AppKit" use xmlLib : script "piyoXML" use scripting additions script myDict property sdefDict : {} end script set (sdefDict of myDict) to {} set aRes to parseSdefAndRetCommandStructure("com.apple.iWork.Keynote", "export") of me on parseSdefAndRetCommandStructure(targAppBundleID, aTargCom) if (sdefDict of myDict) = {} then set aRes to parseSDEFandRetXMLStr(targAppBundleID) of me set (sdefDict of myDict) to (xmlLib’s makeRecordWithXML:aRes) end if set suitesList to ((sdefDict of myDict)’s valueForKeyPath:"dictionary.suite.command") repeat with i in suitesList –SuitesでLoop set j to contents of i try repeat with ii in j –CommandでLoop set jj to contents of ii set tmpName to jj’s attributes’s |name| if aTargCom is in (tmpName as list) then return jj end repeat on error return false end try end repeat return false end parseSdefAndRetCommandStructure on pickUpFromToStr(aStr as string, s1Str as string, s2Str as string) set a1Offset to offset of s1Str in aStr if a1Offset = 0 then return -1 set bStr to text (a1Offset + (length of s1Str)) thru -1 of aStr set a2Offset to offset of s2Str in bStr if a2Offset = 0 then return -2 set cStr to text 1 thru (a2Offset – (length of s2Str)) of bStr return cStr as string end pickUpFromToStr –指定文字と終了文字に囲まれた内容を抽出 on extractStrFromTo(aParamStr, fromStr, toStr) set theScanner to current application’s NSScanner’s scannerWithString:aParamStr set anArray to current application’s NSMutableArray’s array() repeat until (theScanner’s isAtEnd as boolean) set {theResult, theKey} to theScanner’s scanUpToString:fromStr intoString:(reference) theScanner’s scanString:fromStr intoString:(missing value) set {theResult, theValue} to theScanner’s scanUpToString:toStr intoString:(reference) if theValue is missing value then set theValue to "" –>追加 theScanner’s scanString:toStr intoString:(missing value) anArray’s addObject:theValue end repeat if anArray’s |count|() is not equal to 1 then return false return first item of (anArray as list) end extractStrFromTo –SDEFをXincludeを考慮しつつ展開 on parseSDEFandRetXMLStr(targAppBundleID) set thePath to POSIX path of (path to application id targAppBundleID) set aSDEFname to retAppSdefNameFromBundleIPath(thePath, "OSAScriptingDefinition") of me if aSDEFname = false then return if aSDEFname does not end with ".sdef" then set aSDEFname to aSDEFname & ".sdef" set sdefFullPath to thePath & "Contents/Resources/" & aSDEFname set sdefAlias to (POSIX file sdefFullPath) as alias –sdefのフルパスを求める –SDEF読み込み(Xincludeの展開が必要な状態) tell current application set theXML to read sdefAlias as «class utf8» end tell –NSXMLDocumentの生成、Xincludeを有効に set {theXMLDoc, theError} to current application’s NSXMLDocument’s alloc()’s initWithXMLString:theXML options:(current application’s NSXMLDocumentXInclude) |error|:(reference) –XMLを文字データ化 set aDocStr to (theXMLDoc’s XMLData) set aDocStr2 to (current application’s NSString’s alloc()’s initWithData:(aDocStr) encoding:(current application’s NSUTF8StringEncoding)) as string return aDocStr2 end parseSDEFandRetXMLStr –指定パスからアプリケーションのInfo.plist中の属性値を返す on retAppSdefNameFromBundleIPath(appPath as string, aKey as string) set aDict to (current application’s NSBundle’s bundleWithPath:appPath)’s infoDictionary() set aRes to aDict’s valueForKey:(aKey) if aRes = missing value then return false set asRes to aRes as string return asRes as string end retAppSdefNameFromBundleIPath |
More from my site
(Visited 70 times, 1 visits today)