oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

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

A Few Words on Debugging

Spotlight plugin development can be frustrating because the debugging process isn't always that easy. You have to actually install the plugin and monitor its behavior to determine if it's working, which means extra time copying files around, extra time on the command line, etc. Recall from last time the following tools

  • mdls: lists the metadata attributes for the specified file
  • mdfind: finds files matching a given query
  • mdimport: imports file hierarchies into the metadata datastore
  • mdutil: manages the metadata stores used by Spotlight

Of these tools, stick mdimport inside of "code" tags to be absolutely vital. For example, mdimport -d1 ~/Library/ provides feedback that tells you whether or not the proper Spotlight plugin can be located by the server—a fair question to ask. Apple has a very good document on debugging Spotlight plugins available here that you should definitely read.

In Apple’s debugging document, you'll see references to a command called lsregister, which can force the Spotlight server to recognize a bundled Spotlight plugin (like the one just given to you in the ""). By default, however, this command is not in your path. Type the following into Terminal to place a soft link to lsregister in your path.

sudo ln -s /System/Library/Frameworks/ApplicationServices.framework/\
Support/lsregister /usr/bin/lsregister

A Freebie for All of your Hard Work

You may have already noticed an inconvenience. You can't click on the "" file that appears in Spotlight's results and have it open up Stickies, as would be the case for most any other application. This is truly annoying, but nothing a good old-fashioned hack can't fix.

By choosing to "Get Info" on a file, you can set its default application that should open it. The problem then is that there is no default application to do such a thing for type "aaa" files—well not yet. To get Stickies to come front and center when we click on the "" file in Spotlight's search window, we simply need to run a simple AppleScript from within an application bundle. The AppleScript is a one-liner:

tell application "Stickies" to activate

This script opens Stickies if it's not already open, and brings all of its windows to the front, giving us the desired effect even though it's not really "opening" the file at all. All that's left now is to create and associate a bundled utility application that executes the AppleScript with the ".aaa" file type. It's that simple.

Embedding an AppleScript into a Cocoa app is really simple, and you can read about it here. I've used this very approach to create a project called "" The project's source is available here.

One thing that I didn't mention is that you can embed Spotlight plugins into application bundles. If you'd like a compiled version of "" with the "StickiesImporter" plugin already embedded, you can get it here. You'll need copy it to your "Applications" folder and manually run it once to ensure it is "trusted." Once it's in place, go to your ~/Library/ file (which cron should be updating every minute), choose to "Get Info," and set "" for its default application. Make sure to check the box that says "Always Open With" to fully automate the process. (Try clicking on “” in Finder to test). Assuming you've made your one-line update with cron, you're now good to go.

set default app 1

set default app 2
Set the utility application "" to open files of type "aaa" (top) so that Stickies will come front and center when you click on "" in the Spotlight search window (bottom).

Congratulate Yourself

Let's face it, if you've followed this tutorial, you're up to speed on using Spotlight in your Cocoa apps, you can create custom Spotlight plugins (even for the most contrived situations) and you've enjoyed some good hacking along the way. Bravo.

Matthew Russell is a computer scientist from middle Tennessee; and serves Digital Reasoning Systems as the Director of Advanced Technology. Hacking and writing are two activities essential to his renaissance man regimen.

Return to the Mac DevCenter