8
votes

How can I create a spotlight importer for an OS X app that creates a spotlight tool like the calculator spotlight tool? It's the tool where you type in an equation and the calculator app displays the result. I want to do something like that, but I've never tried to use a spotlight importer so I don't know how I'd make that work. Is it possible to do this to begin with?

I don't need specific code, just a general idea of how I'd do that. I might even be thinking wrongly with taking an importer approach, but I'm not sure.

At this point, I don't even need a full answer. Please comment even if all you have is a seemingly inconsequential idea.

2
The docs on developer.apple.com are definitely inadequate here. Lots about supporting custom file formats, nothing about live updating queries as with the calculator. Good question.blahdiblah

2 Answers

5
votes

I'm pretty sure this isn't possible to do with a Spotlight importer, which is the only official interface for Spotlight integration that Apple provides.

A Spotlight importer runs only when Spotlight indexes your files (e.g. when a file of your type changes), not when Spotlight actually runs your search queries, so you have no way to directly "react" on the user's input like the built-in calculator does.

It might be possible to hack into the Spotlight UI with code injection, using something like SIMBL but that would be quite difficult and fragile (could break with system updates, etc.).

1
votes

Alfred is worth a look. Its advanced calculator feature in the free version uses GCMathParser. The user pops up the interface with a hotkey, types the equation, and it displays the result immediately. Pressing enter copies the result to the clipboard and dismisses the box. Pretty slick, in my opinion.

With the Power Pack you can build your own extensions with shell scripts or AppleScripts, though most of them seem to use Growl to display the result. Here's one that sounds similar to what you're trying to accomplish: http://thefeverdog.tumblr.com/post/10308253571/currency-converter

I'm guessing you won't use Alfred because you'd like to build this yourself, but you may find the user interface inspiring nonetheless.