如何以编程方式在mac os x上获取已安装应用程序的列表

时间:2014-06-18 08:37:53

标签: objective-c macos .app

如何通过C代码或Objective-C代码以编程方式在mac os x中安装应用程序?

1 个答案:

答案 0 :(得分:2)

可以使用Spotlight API获取所有应用程序文件。具体来说,NSMetadataQuery类..

-(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]);
    }
}

您有其他各种属性,例如kMDItemFSName。可以找到更多属性here

以上的终端版本是:

mdfind 'kMDItemKind=Application'