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

2023-12-13

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


可以使用聚光灯 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'
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何以编程方式获取 mac os x 上已安装应用程序的列表[重复] 的相关文章

随机推荐