Macos 聚光灯查询
我正在为LEOPARD(10.5)做一个小项目,我是Objective-C编程的新手。我一直在网上搜索一些教程,但我还是很困惑!我需要使用Leopard的spotlight功能来搜索用户计算机上安装的每个.app文件。我还需要它的名称,路径和图标。所有查询的数据必须保存在文本文件中。我该怎么做???Macos 聚光灯查询,macos,spotlight,Macos,Spotlight,我正在为LEOPARD(10.5)做一个小项目,我是Objective-C编程的新手。我一直在网上搜索一些教程,但我还是很困惑!我需要使用Leopard的spotlight功能来搜索用户计算机上安装的每个.app文件。我还需要它的名称,路径和图标。所有查询的数据必须保存在文本文件中。我该怎么做??? 谢谢大家! 定义查询,并观察查询终止 - (void)searchApplications { NSMetadataQuery *query = [[NSMetadataQuery allo
谢谢大家! 定义查询,并观察查询终止
- (void)searchApplications {
NSMetadataQuery *query = [[NSMetadataQuery alloc] init];
query.predicate = [NSPredicate predicateWithFormat:@"kMDItemContentTypeTree == 'com.apple.application'"];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(queryDidFinish:)
name:NSMetadataQueryDidFinishGatheringNotification
object:query];
[query startQuery];
}
在查询终止函数中,循环遍历结果并提取所需的数据
- (void)queryDidFinish:(NSNotification *)notification {
NSMetadataQuery *query = (NSMetadataQuery *)[notification object];
[query stopQuery];
NSMutableArray *paths = [NSMutableArray array];
for(NSMetadataItem *mdItem in query.results) {
NSString *name = [mdItem valueForAttribute:(NSString *)kMDItemDisplayName];
NSString *path = [mdItem valueForAttribute:(NSString *)kMDItemPath];
NSImage *icon = [[NSWorkspace sharedWorkspace] iconForFile:path];
[paths addObject:path];
}
[query release];
[paths writeToFile:@"/tmp/applications.txt" atomically:YES];
}