How can I get list of installed applications on mac os x programmatically [duplicate]

烂漫一生 提交于 2020-01-03 04:49:11

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!