问题
How can I get installed apps in mac os x programmatically either through C code or Objective-C code?
回答1:
It is possible to get all the app files using the spotlight API. Specifically, NSMetadataQuery class..
-(void)doAQuery {
query = [[NSMetadataQuery alloc] init];
// [query setSearchScopes: @[@"/Applications"]]; // If you want to find applications only in /Applications folder
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"kMDItemKind == 'Application'"];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(queryDidFinishGathering:) name:NSMetadataQueryDidFinishGatheringNotification object:nil];
[query setPredicate:predicate];
[query startQuery];
}
-(void)queryDidFinishGathering:(NSNotification *)notif {
int i = 0;
for(i = 0; i< query.resultCount; i++ ){
NSLog(@"%@", [[query resultAtIndex:i] valueForAttribute:kMDItemDisplayName]);
}
}
You have various other attributes, such as kMDItemFSName
. More attributes can be found here
The terminal version of the above is:
mdfind 'kMDItemKind=Application'
来源:https://stackoverflow.com/questions/24280878/how-can-i-get-list-of-installed-applications-on-mac-os-x-programmatically