我想在Apple Mac上提取已安装应用的一些细节。我认为在一堆没有任何额外依赖性的Mac上运行它的最便携方式是使用Applescript。
我可以运行以下命令获取包含plist格式的已安装应用程序的变量:
set theAppsList to do shell script "system_profiler SPApplicationsDataType -xml"
但我找不到告诉Applescript将此文本解析为plist的方法。所有记录的plist示例都显示文件路径以下列形式传递给Applescript:
tell application "System Events" to tell property list file thePropertyListFilePath to ....
但是如何处理我从shell脚本收到的原始plist文本作为plist对象?是否有一些等效的伪代码:
myPlist = new property list (theAppsList)
会在内存中创建一个新的plist对象吗?
答案 0 :(得分:2)
将数据视为XML数据,AppleScript可将其解析为存储在变量中的文本数据。这是我运行do shell script
行时在系统上返回的部分文本数据:
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">
<plist version=\"1.0\">
<array>
<dict>
<key>_SPCommandLineArguments</key>
.
.
.
<key>_items</key>
<array> --> ①
<dict> --> ②
<key>_name</key>
<string>Little Snitch Software Update</string> --> ③
.
.
.
以下AppleScript隔离了标记为①和②的XML元素,并将其数据存储为列表,最终可以从中检索有关每个应用程序的信息(例如标记为③的元素表示第一个应用程序的名称): / p>
tell application "System Events"
-- Creates a new XML data object and stores it in memory
if not (exists XML data "AppData") then ¬
set XMLAppData to make new XML data ¬
with properties ¬
{name:"AppData", text:theAppsList}
-- array element labelled ①
tell XML data "AppData" to ¬
set AppArray to item 2 of (XML elements of ¬
XML element "dict" of XML element "array" of ¬
XML element "plist" whose name is "array")
-- dict element labelled ②
set AppsDataArray to every XML element in AppArray whose name is "dict"
-- The number of applications installed
set n to number of items in AppsDataArray
-- Retrieving ③ from the AppsDataArray list
-- (the name of the first application)
get value of XML element "string" of item 1 in AppsDataArray
end tell
我相信XML data
个对象在闲置约5分钟后会从内存中消失。否则,您可以使用delete XML data "AppData"
或delete every XML data
手动删除它们。