Database Reference
In-Depth Information
Here, we are defining our plug-in as having an MDImporter role, and the list of
UTI s contains just the one for our metadata file. With this change, Mac OS X
knows to use this importer to retrieve the information for our metadata files.
Building the Spotlight Importer
Now that everything is connected, it is time to build the importer. Fortunately,
this is the easiest and shortest part of the entire process. The Spotlight
template created the main.m file that we will be using, and it contains all the
boilerplate code for us. The only code we need to write for the importer is in
the GetMetadataForFile.m file. The template generates a GetMetadataForFile.c file, and
that file will not accept any Objective-C code. Since I prefer Objective-C over
straight C, the first thing I did was rename the .c file to an .m file. This tells
Xcode to compile it as Objective-C rather than C. Since we will be using
Foundation APIs, we need to include Foundation.framework as well.
Spotlight/SpotlightPlugin/GetMetadataForFile.m
#include <CoreFoundation/CoreFoundation.h>
#include <CoreServices/CoreServices.h>
#import <Foundation/Foundation.h>
Boolean GetMetadataForFile( void * thisInterface,
CFMutableDictionaryRef attributes,
CFStringRef contentTypeUTI,
CFStringRef pathToFile)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSDictionary *metadata;
metadata = [NSDictionary dictionaryWithContentsOfFile:(NSString*)pathToFile];
for (NSString *key in [metadata allKeys]) {
[(id)attributes setObject:[metadata objectForKey:key] forKey:key];
}
[pool release], pool = nil;
return TRUE;
}
The actual code for the importer is almost laughably simple. We are just
loading the metadata file back into an NSDictionary , looping over the keys using
the allKeys method, and adding each associated value to the passed-in
CFMutableDictionaryRef . Once we are done with the NSDictionary , we return TRUE and
are done. Since we are running inside a C function, we need to wrap the entire
procedure in an NSAutoreleasePool so that we are not leaking any memory.
Testing the Spotlight Importer
There are a couple of ways to test the importer to make sure everything is
working properly. The first thing we need to do is generate the metadata files,
 
 
 
Search WWH ::




Custom Search