我只是想知道有人会如何找到安装在 Mac OS X 10.5 上的所有应用程序,最好使用 applescript 并将所有应用程序名称输出到文本文件。
Mac OS X 下安装的所有应用程序都在Launch Services 数据库中注册。
Launch Services 框架包含一个辅助 shell 命令
lsregister
,除其他用途外,它还可以转储存储在 Launch Services 数据库中的信息。在 Mac OS X 10.5 和 10.6 下,该命令位于以下文件夹中:
/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister
使用一些简单的 grep 过滤器可以提取所有已注册应用程序的完整路径:
lsregister -dump | grep --after-context 1 "^bundle" | grep --only-matching "/.*\.app"
将它们放在一起,以下 AppleScript 将使用 info for 命令计算所有已注册应用程序的用户可见名称:
property pLSRegisterPath : "/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister"
set theAppPaths to every paragraph of (do shell script pLSRegisterPath & " -dump | grep --after-context 1 \"^bundle\" | grep --only-matching \"/.*\\.app\"")
set theNames to {}
repeat with thePath in theAppPaths
try
copy displayed name of (info for (thePath as POSIX file)) to end of theNames
end try
end repeat
choose from list theNames
这篇文章中有几种方法,具体取决于您希望搜索的深度。也不确定这是否正是您想要的输出格式,但您可以根据您的特定需求对其进行调整。
我使用 system_profiler 命令来获取我的文本。然后你可以根据需要进行解析。
system_profiler SPApplicationsDataType
...
AppleScript Utility:
Version: 1.1.1
Last Modified: 5/18/09 10:34 PM
Kind: Intel
64-Bit (Intel): Yes
Location: /System/Library/CoreServices/AppleScript Utility.app
也许将其通过管道传输到文本文件,然后使用 sed ....
如果你想有一个应用程序,可以通过 applescript 调用 Bash 命令,或者你可以用 .command 扩展名保存脚本,用户将能够双击它。
对于像我这样使用 bash 脚本来实现他们目标的人来说,这里是脚本的 bash 变体:
#!/usr/bin/env bash
path='/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support'
$path/lsregister -dump | grep -A 1 "^bundle" | grep --only-matching "/.*\.app" | awk -F "/" '{ print $NF }' | awk -F "." '{ print $1 }'
这给出了所有不带 .app 扩展名的应用程序的列表。
AsObjC 解决方案:
use AppleScript version "2.5" -- EI Capitan and later
use framework "Foundation"
use scripting additions
set plistString to do shell script "system_profiler -xml SPApplicationsDataType"
set dataString to current application's NSString's stringWithString:plistString
set plistData to dataString's dataUsingEncoding:4
set propertyList to current application's NSPropertyListSerialization's propertyListWithData:plistData options:0 format:(missing value) |error|:(missing value)
set allItems to (propertyList's objectAtIndex:0)'s objectForKey:"_items"
set theDictionary to allItems's dictionaryWithValuesForKeys:{"_name", "version", "path", "arch_kind", "obtained_from", "lastModified", "signed_by"}
-- |version| of theDictionary as list -- versions list
-- |path| of theDictionary as list -- Posix paths list
-- arch_kind of theDictionary as list -- architecture list
-- obtained_from of theDictionary as list -- developer name list
-- (lastModified of theDictionary) as list -- lastModified dates list
-- signed_by of theDictionary as list -- software signings list of lists
_name of theDictionary as list -- app names list
注意:我在已安装的应用程序名称列表上方。取消注释相应的代码行以获取版本、Posix 路径(等等)列表。