基本问题
我正在寻找一种解决方案来枚举 (Mac) OS X 系统上安装的所有应用程序,即使用 LaunchServices 注册的所有应用程序包。 .
简单的方法不起作用
(请注意:lsregister
可以在 /System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister
下找到)。
目前我正在使用 lsregister -dump | grep -E '^.*[ \\t]*path:[ \\t]+(\/.*)$' | grep -Eo '\/.*'
的输出,有一些问题:
-
.app
带有换行符的包(\n
,在 UNIX 上有效)未正确处理(此问题可以通过编写适当的解析器来修复) lsregister
无法正确处理奇怪的文件名(例如包含 RTL mark )。lsregister
似乎只是忽略名称中带有 RTL 标记的文件。 如果需要,您可以通过运行以下命令来创建名称中包含 RTL 的文件。python -c 'import shutil; shutil.copytree(u"/Applications/TextEdit.app", u"/Applications/ThisIsAWeird\u202EApp.app")'
执行此操作后,
lsregister -dump | grep ThisIsAWeird
不会透露它。但是,当您右键单击文本文件并转到“打开方式”时,LaunchServices 和 Finder 仍然支持该文件。
简单地枚举 /Applications
中的所有文件和其他目录不起作用,因为 LaunchServices 知道任意文件夹中的应用程序包,并且我也需要这些应用程序。
我没有开始工作的其他方法
显然,有一个名为_LSFindApplications
的私有(private)API函数这正是我需要做的,但我无法让它工作,并且在互联网上没有找到任何非官方文档。 (证明它存在:nm /System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/LaunchServices | grep __LSFindApplications$
)
更新:system_profiler 方法似乎也不起作用
到目前为止,我认为 system_profiler
@Anoop 建议的方法有效,但今天我发现 lsregister
仍然会获取更多的二进制文件。有些或在所有机器上都缺失,有些似乎只在 system_profiler
中缺失当您使用另一个 OS X 安装安装了外部卷时。以下是 system_profiler
的输出中缺少的内容的列表。 (但存在于 lsregister
中)在我的一台机器上:
/Applications/Automator.app/Contents/Resources/Application Stub.app
/Applications/Google Chrome.app/Contents/Versions/30.0.1599.69/Google Chrome Framework.framework/Resources/app_mode_loader.app
/Applications/Google Chrome.app/Contents/Versions/30.0.1599.69/Google Chrome Framework.framework/Resources/crash_report_sender.app
/Applications/Google Chrome.app/Contents/Versions/30.0.1599.69/Google Chrome Helper EH.app
/Applications/Google Chrome.app/Contents/Versions/30.0.1599.69/Google Chrome Helper NP.app
/Applications/Google Chrome.app/Contents/Versions/30.0.1599.69/Google Chrome Helper.app
/System/Library/CoreServices/AddPrinter.app
/System/Library/CoreServices/AirPlayUIAgent.app
/System/Library/CoreServices/AppleFileServer.app
/System/Library/CoreServices/AppleGraphicsWarning.app
/System/Library/CoreServices/AppleScript Runner.app
/System/Library/CoreServices/AppleScript Utility.app
/System/Library/CoreServices/CalendarFileHandler.app
/System/Library/CoreServices/Certificate Assistant.app
/System/Library/CoreServices/CoreLocationAgent.app
/System/Library/CoreServices/CoreServicesUIAgent.app
/System/Library/CoreServices/CoreTypes.bundle
/System/Library/CoreServices/Database Events.app
/System/Library/CoreServices/DiskImageMounter.app
/System/Library/CoreServices/Expansion Slot Utility.app
/System/Library/CoreServices/File Sync.app
/System/Library/CoreServices/FileSyncAgent.app
/System/Library/CoreServices/Folder Actions Dispatcher.app
/System/Library/CoreServices/Folder Actions Setup.app
/System/Library/CoreServices/HelpViewer.app
/System/Library/CoreServices/Image Events.app
/System/Library/CoreServices/Install in Progress.app
/System/Library/CoreServices/Jar Launcher.app
/System/Library/CoreServices/Java Web Start.app
/System/Library/CoreServices/KeyboardSetupAssistant.app
/System/Library/CoreServices/Language Chooser.app
/System/Library/CoreServices/LocationMenu.app
/System/Library/CoreServices/MRTAgent.app
/System/Library/CoreServices/Memory Slot Utility.app
/System/Library/CoreServices/NetAuthAgent.app
/System/Library/CoreServices/Network Diagnostics.app
/System/Library/CoreServices/Network Setup Assistant.app
/System/Library/CoreServices/ODSAgent.app
/System/Library/CoreServices/Pass Viewer.app
/System/Library/CoreServices/Printer Setup Utility.app
/System/Library/CoreServices/RegisterPluginIMApp.app
/System/Library/CoreServices/Screen Sharing.app
/System/Library/CoreServices/SecurityAgent.app
/System/Library/CoreServices/SecurityFixer.app
/System/Library/CoreServices/SocialPushAgent.app
/System/Library/CoreServices/System Events.app
/System/Library/CoreServices/System Image Utility.app
/System/Library/CoreServices/SystemUIServer.app
/System/Library/CoreServices/Ticket Viewer.app
/System/Library/CoreServices/UniversalAccessControl.app
/System/Library/CoreServices/UnmountAssistantAgent.app
/System/Library/CoreServices/UserNotificationCenter.app
/System/Library/CoreServices/VoiceOver.app
/System/Library/CoreServices/ZoomWindow.app
/System/Library/CoreServices/backupd.bundle/Contents/Resources/TMLaunchAgent.app
/System/Library/CoreServices/loginwindow.app
/System/Library/CoreServices/rcd.app
/System/Library/Frameworks/PubSub.framework/Versions/A/Resources/PubSubAgent.app
/System/Library/Frameworks/Quartz.framework/Versions/A/Frameworks/QuickLookUI.framework/Versions/A/Resources/QuickLookUIHelper.app
/System/Library/Frameworks/QuickLook.framework/Versions/A/Resources/quicklookd.app
/System/Library/PrivateFrameworks/DiskImages.framework/Versions/A/Resources/DiskImages UI Agent.app
/System/Library/Services/AppleSpell.service
/System/Library/Services/ChineseTextConverterService.app
/System/Library/Services/ImageCaptureService.app
/System/Library/Services/OpenSpell.service
/System/Library/Services/SpeechService.service
/System/Library/SocialServices/Facebook.socialplugin
/System/Library/SocialServices/Twitter.socialplugin
/System/Library/SocialServices/Weibo.socialplugin
最佳答案
首先,你的要求不太正确:
I'm looking for a solution to enumerate all applications installed on a (Mac) OS X system, i.e. all application bundles registered with LaunchServices.
这些不是同一件事。使用 LaunchServices 注册的应用程序包是 lsregister -dump 的输出。这与“安装的所有应用程序”不同。没有“安装的所有应用程序”的确切查询。 OS X 甚至根本没有这样的概念。
system_profiler
的输出与 Spotlight 所了解的内容相同。这并不完全是已安装应用程序的完整列表,并且在某些情况下可能不正确。例如,Spotlight 可能已被禁用,或者它可能尚未完成第一次扫描。 (不支持询问 Spotlight 是否已完成第一次扫描;雷达 15177626。)
还有 pkgutil --pkgs
将为您提供额外的“已安装的东西”。它也不完整,并且可能列出未安装的内容。
您还可以使用mdfind
遍历整个磁盘并查看什么自称为应用程序。这非常有效,但速度很慢。
您还可以使用locate .app | grep '\.app$'
如果定位数据库是最新的并且您只需要应用程序包,这非常有效。
我在 WWDC 上向 Apple 询问了此事。没有简单的、受支持的答案。打开雷达。您可以要求欺骗15177489。
关于macos - 枚举 OS X 上所有已安装的应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15164132/