在OS X上枚举所有已安装的应用程序

Joh*_*iss 7 macos launch-services osx-mountain-lion

基本问题

我正在寻找一个解决方案来枚举安装在(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标记).lsregister似乎忽略了名称中带有RTL标记的文件.如果需要,可以通过运行以下命令在名称中创建一个RTL文件.

    python -c 'import shutil; shutil.copytree(u"/Applications/TextEdit.app", u"/Applications/ThisIsAWeird\u202EApp.app")'
    
    Run Code Online (Sandbox Code Playgroud)

    这样做了以后,lsregister -dump | grep ThisIsAWeird不会透露它.但是,当您右键单击文本文件并转到"打开方式"时,您将启动LaunchServices和Finder仍然支持该文件. 看你自己

简单地枚举所有文件/Applications和其他目录都不起作用,因为LaunchServices知道任意文件夹中的应用程序包,我也需要这些应用程序.

其他我没有开始工作的approch

显然,有一个私有的API函数_LSFindApplications,它正是我需要做的,但我不能让它工作,并没有在互联网上找到任何非官方的文档.(证明它存在: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
Run Code Online (Sandbox Code Playgroud)

Rob*_*ier 6

首先,你的要求不太对:

我正在寻找一种解决方案来枚举安装在 (Mac) OS X 系统上的所有应用程序,即所有使用 LaunchServices 注册的应用程序包。

这些不是一回事。向 LaunchServices 注册的应用程序包是lsregister -dump. 这与“安装的所有应用程序”不同。没有对“安装的所有应用程序”的确切查询。OS X 甚至真的没有这样的概念。

的输出system_profiler相当于 Spotlight 所知道的。这并不完全是已安装应用程序的完整列表,并且在某些情况下可能不正确。例如,Spotlight 可能已被禁用,或者它可能尚未完成第一次扫描。(不支持询问 Spotlight 是否已完成第一次扫描;雷达 15177626。)

还有pkgutil --pkgs哪个将为您提供额外的“已安装的东西”。它也不完整,可能会列出未安装的内容。

您还可以使用mdfind遍历整个磁盘并查看自称为应用程序的内容。这非常有效,但速度很慢。

locate .app | grep '\.app$'如果 locate 数据库是最新的并且您只想要应用程序包,您也可以使用它非常有效。

我在 WWDC 上向 Apple 询问了这个问题。没有简单的、受支持的答案。打开雷达。您可以要求欺骗 15177489。