macdevcenter.com
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Programming with Spotlight

by Matthew Russell
07/12/2005

The API for Spotlight offers highly advanced search capabilities. In fact, you can develop some of the very features of Tiger we've already grown to love using Spotlight's API. In this piece, we'll ease into Spotlight programming from a Cocoa development perspective so that you can make your applications Spotlight enabled. Next time (this coming Friday), we'll finish our work with Spotlight by hacking up a plugin for Stickies.

Pregame

As with any endeavor, an adequate background is vital to success. Apple has provided quite a bit of documentation on Spotlight, and it is very good, although not quite final. This writing assumes that you've done a bit of Cocoa programming before and understand how Spotlight works at a conceptual level. If you need a quick crash course, review:

We'll ease into the Cocoa programming initially, but will quickly increase the pace because there's a lot of turf to cover with Spotlight specifics. If you need more context on general purpose Cocoa programming than Apple's developer documentation, check out one of the many excellent tutorials here on MacDevCenter. Without further adieu, sit back, relax, and strap on your seat belt.

Developing with Spotlight

Related Reading

Cocoa in a Nutshell
A Desktop Quick Reference
By Michael Beam, James Duncan Davidson

As a developer, you can interact with Spotlight in a variety of ways. Here are a few of the most common ways:

  • Use Carbon- or Cocoa-level function calls from within your compiled application
    • To have your application display the Spotlight search window
    • To directly examine the metadata of a specific file
    • To query the Spotlight server for specific metadata constraints on an operating system-wide level
  • Use a command line tool from within a script
    • Parse the output of an existing metadata tool such as mdls or mdfind
    • Create your own customized command line tool that performs a task of your own choosing
  • Create a plugin
    • Allow Spotlight to use the metadata available from your own application's custom file types
    • Create a plugin for an existing file type for which there's not any available plug-in

We'll work though each of these possibilities, and you'll soon be able to interact with Spotlight on a variety of levels. Let's get our hands dirty with some code by building a sample application, looking at some of Apple's examples, and reviewing some of the command line tools.

Displaying the Spotlight Search Window

Since Cocoa is the drink of the day (as always), let's make an example project that interacts with Spotlight. If you haven't already, now is a good time to update to Xcode 2.1. In Xcode:

  • Create a new project
    • Open Xcode
    • Create a new "Cocoa Application"
    • Name it "SpotlightExamples" and save it somewhere
With a template in place, let's proceed to create a controller class since we're big fans of the Model View Controller paradigm
  • Create a controller class
    • From Xcode's "File" menu, choose "New File"
    • Pick "Objective-C class"
    • Name it "Controller" and choose to also create the header
    • In Xcode's "Groups & Files" pane, drag the Controller files into the "Classes" folder

new project
Create a new project in Xcode

Pages: 1, 2, 3, 4

Next Pagearrow