Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cocoa get list of installed applications

Is there a way to get all installed applications for the current user in cocoa?

NSArray *runningApps = [[NSWorkspace sharedWorkspace] launchedApplications];

The above gives me currently running applications but for my app I need to list all installed applications. I need the application key (e.g. com.apple.appname) so system_profiler will does not work.

like image 426
Kamran224 Avatar asked Jan 13 '14 12:01

Kamran224


1 Answers

For OSX, the key library for gathering information about launchable applications is Launch Services (see Apple's Launch Services Programming Guide), which will give you the information about an application such as bundle id, file types that it accepts, etc.

For actually locating all executables on the machine, you're going to want to use Spotlight in one form or the other (either the API or by calling out to mdfind).

Example of using the command line version:

mdfind "kMDItemContentType == 'com.apple.application-bundle'"

will return a list of all application paths.

Using a similar term in the spotlight API will result in an appropriate list, from which you can then either open the main bundle using NSBundle or use Launch Services to retrieve information about the app.

I don't have time to do a thorough test of this, but the basic code would be:

NSMetadataQuery *query = [[NSMetadataQuery alloc] init];        
[query setSearchScopes: @[@"/Applications"]];  // if you want to isolate to Applications
NSPredicate *pred = [NSPredicate predicateWithFormat:@"kMDItemContentType == 'com.apple.application-bundle'"];

// Register for NSMetadataQueryDidFinishGatheringNotification here because you need that to
// know when the query has completed

[query setPredicate:pred];
[query startQuery]; 

(Revised to use @John's localization-independent query instead of my original)

like image 130
gaige Avatar answered Oct 22 '22 05:10

gaige