oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Creating Spotlight Plugins
Pages: 1, 2, 3, 4

Copy in Supporting Project Files

In order to read the StickiesDatabase format, you'll either need to reverse engineer the format yourself (you can read about the process here) or you can benefit from someone else's hard work and use open source code released under the GNU GPL that's available here. Simply save these files to disk, navigate to them in Finder, and copy them into your project by dragging them into your "Classes" folder in Xcode. Make sure the box is checked that specifies to copy them into the project. (Actually, only a small portion of the "Document" class is really needed, but the others are left there for convenience in case you decide to be creative and use them for other things.)

Implement the GetMetadataForFile Function

Our last big step is to implement the GetMetadataForFile function; but as you'll see, it's not much of a chore at all. Here's the function's body:

Boolean GetMetadataForFile(
    void* thisInterface, 
    CFMutableDictionaryRef attributes, 
    CFStringRef contentTypeUTI,
    CFStringRef pathToFile)
    Boolean success=NO;
    NSAutoreleasePool *pool;

    // Don't assume that there is an autorelease pool 
    //around the calling of this function.
    pool = [[NSAutoreleasePool alloc] init];
    /* Pull any available metadata from the file */
    //The Document class provides the methods necessary to 
    //manipulate the StickiesDatabase file
    NSMutableArray *array = [[NSMutableArray alloc] initWithArray:
        [NSUnarchiver unarchiveObjectWithFile:

    [array autorelease];
    NSEnumerator *enumerator = [array objectEnumerator];
    Document *d;
    NSMutableArray* tempArray = [NSMutableArray array];

    while (d = [enumerator nextObject]) { 
        NSString *t = [d stringValue];
        [tempArray addObject:t];
    [(NSMutableDictionary *)attributes setObject:tempArray

    // return YES so that the attributes are imported and
    // add more resilient error handling at your own discretion

    //memory management
    [pool release];
    return success;

I'm sure you'll agree that this method is trivial. This plugin will only process files ending in "aaa," which we're assuming to be in a specific format. The text of each note is simply read and stored into an array. The array is loaded into a dictionary, mapped to the key specified in our schema.xml file, and passed to the Spotlight server, where the rest of the work is handled. It's noteworthy to clarify that our "metadata" is the complete text of each note. Recall that the intention of this plugin is to make the text of each note searchable in Spotlight.

Make Minor Changes to Reflect our Use of Cocoa

At this point, all of the conceptual Spotlight work is done. Now we just need to take care of a few details to ensure our project compiles and links properly:

  • Add and include the "Foundation" framework
    • Ctrl-click on "External Frameworks and Libraries", choose "Add" -> "Existing Frameworks...", and select "Foundation.framework"
    • Add a #import <Foundation/Foundation.h> to the top of "GetMetadataForFile.c"
  • Include the Document class in the source code
    • Add a #import "Document.h" to the top of "GetMetadataForFile.c"
  • Add the "Cocoa" framework (because it's included in the Document class)
    • Ctrl-click on "External Frameworks and Libraries," choose "Add" -> "Existing Frameworks," and select "Cocoa.framework"
  • Rename GetMetadataForFile.c and main.c to have a .m extension
    • We're using and linking Objective-C code, so you'll get a mysterious "Cannot find function pointer MetadataImporterPluginFactory" error if you don't rename.

At this point, your code should build just fine. Making sure you've modified cron's schedule to copy and rename the "StickiesDatabase" file, you can now go into your project's "build" folder and copy your importer into place with a sudo cp -r StickiesImporter.mdimporter /Library/Spotlight from Terminal. If you want it, the complete XCode project file is available here.

xcode 2.0 bug
Side note: In Xcode 2.0, there's a bug that doesn't allow you to change out of a "Development" build style when double clicking on the blue application icon and trying to do so from the "Styles" tab. For changes to build style to stick, you need to make them from the "Project" menu.

With your plugin in place, you should now be able to find text in Stickies notes from Spotlight. (If things don't turn out as expected, there are some troubleshooting references below.) Type some unique practice text such as "ABCDEFG" into a note to check it out, keeping in mind there will be at most a one-minute lag because cron has to do its thing.

our plugin in action
Our plugin at work!

Pages: 1, 2, 3, 4

Next Pagearrow