本文介绍了枚举OS X上所有已安装的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种解决方案,以枚举在(Mac)OS X系统上安装的所有应用程序,即,已在启动服务.

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.

(请注意:lsregister可在/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister下找到).

(Please note: lsregister can be found under /System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister).

当前,我正在使用lsregister -dump | grep -E '^.*[ \\t]*path:[ \\t]+(\/.*)$' | grep -Eo '\/.*'的输出,它有一些问题:

Currently I am using the output of lsregister -dump | grep -E '^.*[ \\t]*path:[ \\t]+(\/.*)$' | grep -Eo '\/.*', which has a few problems:

  • .app带有换行符的捆绑软件(\n,在UNIX上有效)未正确处理(可以通过编写适当的解析器来解决此问题)
  • lsregister无法正确处理奇怪的文件名(例如,包含 RTL标记). lsregister似乎忽略了名称中带有RTL标记的文件.如果需要,可以通过运行以下命令来创建名称为RTL的文件.

  • .app bundles with newlines (\n, which is valid on UNIX) are not handled correctly (This issue would be fixable by writing a proper parser)
  • lsregister doesn't handle correctly weird filename (e.g. containing the RTL mark). lsregister just seems to ignore files with the RTL mark in their name.If you want to, you can create a file with RTL in its name by running the following command.

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

执行完此操作后,lsregister -dump | grep ThisIsAWeird显示它.但是,当您右键单击一个文本文件并转到打开方式"时,LaunchServices和Finder仍然支持该文件.

After doing this, lsregister -dump | grep ThisIsAWeird will not reveal it. But when you right click on a text file and go to "Open With" you'll that LaunchServices and Finder still support that file.

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

Simply enumerating all files in /Applications and other directories does not work because LaunchServices is aware of application bundles in arbitrary folders and I also need these apps.

显然,有一个名为_LSFindApplications的私有API函数,它完全可以执行我需要做的事情,但是我无法使其正常工作,并且在互联网上找不到任何非正式的文档. (证明存在:nm /System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/LaunchServices | grep __LSFindApplications$)

Apparently, there's a private API function called _LSFindApplications which does exactly what I need to do but I can't get it to work and didn't find any inofficial documentation on the internet. (proof it exists: nm /System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/LaunchServices | grep __LSFindApplications$)

直到现在,尽管@Anoop建议的system_profiler方法有效,但是今天我发现lsregister仍然可以拾取更多的二进制文件.在所有计算机上有些或缺少某些东西,当您用另一安装的OS X挂载了一个外部卷时,某些似乎只在system_profiler中缺少了.这是system_profiler输出中缺少的列表(但在)在我的一台机器上:

Until now I though the system_profiler method as suggested by @Anoop works but today I found out that lsregister still picks up more binaries. Some or missing on all machines, some seems to be only missing in system_profiler when you have mounted an external volume with another installation of OS X. Here's a list what's missing in the output of system_profiler (but is present in lsregister) on one of my machines:

/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

推荐答案

首先,您的要求不太正确:

First, your request is not quite right:

这些不是一回事.向LaunchServices注册的应用程序捆绑包是lsregister -dump的输出.这与已安装所有应用程序"不同.没有确切的查询已安装所有应用程序". OS X甚至根本没有这样的概念.

Those are not the same thing. The application bundles registered with LaunchServices is the output of lsregister -dump. That is not the same thing as "all applications installed." There is no exact query for "all applications installed." OS X does not even really have such a concept.

system_profiler的输出与Spotlight知道的等效.那不完全是已安装应用程序的完整列表,并且在某些情况下可能不正确.例如,Spotlight可能已被禁用,或者可能尚未完成第一次扫描. (没有支持的方式来询问Spotlight是否已完成第一次扫描;雷达15177626.)

The output of system_profiler is equivalent to what Spotlight knows about. That's not exactly the full list of installed applications, and may not be correct in several cases. Spotlight may have been disabled, for instance, or it may not have completed its first scan. (There is no supported way to ask Spotlight if it has finished its first scan; radar 15177626.)

还有pkgutil --pkgs,它将为您提供其他已安装的物品".它也不完整,可能会列出未安装的东西.

There is also pkgutil --pkgs which will provide you additional "installed things." It isn't complete either, and may list things that are not installed.

您还可以使用mdfind遍历整个磁盘,并查看将其称为应用程序的内容.这很有效,但是很慢.

You can also use mdfind to walk the whole disk and see what calls itself an application. This is pretty effective, but slow.

您还可以使用locate .app | grep '\.app$',如果locate数据库是最新的并且您只需要应用程序捆绑包,这将非常有效.

You can also use locate .app | grep '\.app$' which is pretty effective if the locate database is up to date and you only want app bundles.

我在WWDC上问了苹果这件事.没有简单的受支持的答案.打开雷达.您可以要求欺骗15177489.

I asked Apple about this at WWDC. There is no simple, supported answer. Open a radar. You can ask to dupe 15177489.

这篇关于枚举OS X上所有已安装的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-15 23:09