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

pav*_*mvn 2 macos objective-c .app

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

Sha*_*i K 5

可以使用 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]);
    }
}
Run Code Online (Sandbox Code Playgroud)

您还有各种其他属性,例如kMDItemFSName. 更多属性可以在这里找到

上面的终端版本是:

mdfind 'kMDItemKind=Application'
Run Code Online (Sandbox Code Playgroud)