2012-01-31 51 views
0

我正在爲LEOPARD(10.5)開發一個小型項目,而且我是一位Objective-C編程的新手。我一直在網上搜索一些教程,但我仍然感到困惑!我需要使用Leopard的聚光燈功能來搜索用戶計算機上安裝的每個.app文件。我也需要它的名字,路徑和圖標。所有查詢的數據必須保存在文本文件中。我怎樣才能做到這一點??? 謝謝!用聚光燈詢問

回答

2

定義查詢,並觀察查詢終止。

- (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]; 
} 
+0

非常感謝你= D – 2012-03-09 12:20:52