8

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.

Carter Pape
  • 1,009
  • 1
  • 17
  • 40
  • 1
    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 May 18 '12 at 20:11

2 Answers2

5

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.).

omz
  • 53,243
  • 5
  • 129
  • 141
  • This answer makes me sad. I had a really great idea for a spotlight tool, and now it's looking like I can't do it. Oh well, I'll probably end up giving you the bounty, but I really want to believe that somebody can come up with a solution, so I'll wait until the last day to award you. Thanks for the explanation of how Spotlight Importers work, it was quite helpful in explaining basically what they are. – Carter Pape May 19 '12 at 14:24
1

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.

paulmelnikow
  • 16,895
  • 8
  • 63
  • 114
  • This is the kind of answer that I was looking for after @omz answered; I was missing the inspiration that I felt when I first had the idea for this app, but seeing Alfred run has reinvigorated that inspiration. I do want to reward omz somehow for input, so I'll give him the answer acceptance and you the bounty. – Carter Pape May 24 '12 at 19:44
  • Thanks! I'm glad it complemented omz's answer. – paulmelnikow May 25 '12 at 13:57