如何以编程方式获取Mac OS X上已安装的应用程序的列表 [英] How can I get list of installed applications on mac os x programmatically
本文介绍了如何以编程方式获取Mac OS X上已安装的应用程序的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何通过C代码或Objective-C代码以编程方式在Mac OS X中安装已安装的应用程序?
How can I get installed apps in mac os x programmatically either through C code or Objective-C code?
推荐答案
可以使用Spotlight API获取所有应用程序文件.具体来说,是NSMetadataQuery类.
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]);
}
}
您还有其他各种属性,例如 kMDItemFSName
.在此处
You have various other attributes, such as kMDItemFSName
. More attributes can be found here
上述终端版本为:
mdfind 'kMDItemKind=Application'
这篇关于如何以编程方式获取Mac OS X上已安装的应用程序的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文