// Method invoked when the initial query gathering is completed
- (void)initalGatherComplete:sender;
{
// Stop the query, the single pass is completed.
[self.metadataSearch stopQuery];
// Iterate the results and find our info.
NSUInteger i=0;
for (i=0; i < [self.metadataSearch resultCount]; i++) {
NSMetadataItem *theResult = [self.metadataSearch resultAtIndex:i];
NSString *appPath = [theResult valueForAttribute:(NSString *)kMDItemPath];
// Use launch services to retrieve info and filter out invisibles and aliases
// Launch Services is CF so we need to bridge to a CFURLRef
CFURLRef appURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (__bridge CFStringRef)appPath, kCFURLPOSIXPathStyle, YES);
// Create our LSItemInfoRecord
LSItemInfoRecord itemInfoRecord;
// Pass in our CFURLRef and filter out invisibles and aliases
LSCopyItemInfoForURL(appURL, kLSRequestBasicFlagsOnly | kLSRequestAppTypeFlags, &itemInfoRecord);
//We don't need the appURL anymore so set it free
CFRelease(appURL);
if ((itemInfoRecord.flags & kLSItemInfoIsApplication) &&
!(itemInfoRecord.flags & kLSItemInfoIsInvisible) &&
!(itemInfoRecord.flags & kLSItemInfoIsAliasFile))
{
// Make some strings to hold our data
NSString *nameString = nil, *versionString = nil;
// Make sure it really is an app bundle
if (itemInfoRecord.flags & kLSItemInfoIsContainer)
{
// Get info for app bundles
// Load in the Info.plist and read the keys
NSDictionary *infoDictionary = [NSDictionary dictionaryWithContentsOfFile: [appPath stringByAppendingPathComponent: @"/Contents/Info.plist"]];
nameString = infoDictionary[@"CFBundleName"];
versionString = infoDictionary[@"CFBundleShortVersionString"];
if (!versionString)
{
versionString = infoDictionary[@"CFBundleVersion"];
}
//Add our info to a mutable dict.
NSDictionary *dict = @{@"appName": nameString,
@"appVersion": versionString};
}
}
}
// Remove the notifications to clean up after ourselves.
// Also release the metadataQuery.
// When the Query is removed the query results are also lost.
[[NSNotificationCenter defaultCenter] removeObserver:self
name:NSMetadataQueryDidUpdateNotification
object:self.metadataSearch];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:NSMetadataQueryDidFinishGatheringNotification
object:self.metadataSearch];
self.metadataSearch=nil;
//Stop our timer and figure how long that took
NSTimeInterval totalTime;
self.stopDate = [NSDate timeIntervalSinceReferenceDate];
self.theText = [NSString stringWithFormat:@"App search took: %.4f seconds", (totalTime = self.stopDate - self.startDate)];
}