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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With