Enumerate All Installed Applications on OS X

2019-01-18 00:29发布

问题:

Basic problem

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.

Simple approach that did not work

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

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

  • .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")'
    

    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.

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.

Other approch that I didn't get to work

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$)

UPDATE: system_profiler method also doesn't seem to work

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

回答1:

First, your request is not quite right:

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.

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.

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.)

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.

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

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.

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



回答2:

in terminal:

system_profiler SPApplicationsDataType > list_of_all_apps.txt

... writes output of the system_profiler query to that text file.



回答3:

What about find using Cocoa application/code as done here.

Also, you might be able use to use system_profiler -xml SPApplicationsDataType and then parse the resulting XML data to get just the applications that you are interested in.