Bundle IDで指定したアプリケーションのSDEF(AppleScript定義辞書)を、Xincludeを考慮しつつparseして、XPathで指定したクラスにアクセスするAppleScriptです。
KeynoteのAppleScript用語辞書の「open」コマンド、
を指定して本Scriptでデータを取り出すと、
のような出力が得られます。まだちょっと、属性値を取り出すところまで行っていませんが、つまり…指定コマンドのパラメータや、指定クラスがどのようなコマンドに応答するかなど、プログラム側からわかるようになることでしょう(多分)。
AppleScript名:Bundle IDで指定したアプリケーションのSDEFからXPathで指定したClassにアクセス v2.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 scripting additions set targAppBundleID to "com.apple.iWork.Keynote" –SDEFを取り出すターゲットのアプリケーションのBundle ID –set commandXPath to "//class[@name=’application’]/property" –set commandXPath to "//command[@name=’open’]/direct-parameter" –set commandXPath to "//class[@name=’document’]/property" –set commandXPath to "//class[@name=’document’]/responds-to" –set commandXPath to "//command[@name=’open’]" –Open Command–Open コマンド set commandXPath to "//command[@name=’open’]/direct-parameter/type" –Open Command–Open コマンド set aRes to parseSDEFandRetXPathData(targAppBundleID, commandXPath) of me return aRes –SDEFをXincludeを考慮しつつ展開 on parseSDEFandRetXPathData(targAppBundleID, commandXPath) 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 set dRes to my extractFrom:theXMLDoc matchingXPath:commandXPath return dRes end parseSDEFandRetXPathData –指定のNSXMLDocumentから、指定のXPathでアクセスして内容を返す on extractFrom:theNSXMLDocument matchingXPath:theQuery set attStrings to {} — where attributes will be stored set theXMLOutput to current application’s NSXMLElement’s alloc()’s initWithName:"output" — found nodes added to this set {theResults, theNSError} to (theNSXMLDocument’s nodesForXPath:theQuery |error|:(reference)) — query if theResults is not missing value then repeat with anNSXMLNode in (theResults as list) anNSXMLNode’s detach() — need to detach first if anNSXMLNode’s |kind|() as integer = current application’s NSXMLAttributeKind then — see if it’s an attribute or node set end of attStrings to (anNSXMLNode’s stringValue()) as {text, list, record} else (theXMLOutput’s addChild:anNSXMLNode) — add node end if end repeat return (theXMLOutput’s XMLStringWithOptions:(current application’s NSXMLNodePrettyPrint)) as {text, list, record} else return missing value end if end extractFrom:matchingXPath: –指定パスからアプリケーションの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 64 times, 1 visits today)
2022年に書いた価値あるAppleScript – AppleScriptの穴 says:
[…] […]