쿼리를 정의하고 쿼리 종료를 관찰하십시오.
- (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];
}
출처
2012-03-06 18:23:04
nst
대단히 감사합니다. = D –