Spotlight Integration Take 1 by Seth Willits
August 13, 2005

Using New Features From REALbasic 2005!
In this tutorial, we're going to primarily use a new feature from REALbasic 2005 which is the new integration with Spotlight in Mac OS X 10.4. Spotlight is a very advanced beast and REALbasic's implementation of the feature is simple and easy to use (though it has it's annoying quirks [that almost make it useless] as you'll see). We'll take a look at how to use it as well as a neat little trick that displays the Spotlight window in your app.



SpotlightQuery Class
This is the heart of the integration. All you have to do is create an instance, set the Query property, and call the Run method. Voila! What happens next is the Changed event is called several times which signals that results have been added, removed, or somehow changed. I don't want to go into too much detail with a good project yet, because I'm extremely unsatisfied with the results I've come up with. It takes 1 second for Spotlight to find 22,000 files on my hard drive, but it takes REALbasic's ListBox 11.7 seconds to display the results (during which the application is completely locked up). If I try to display them live (as they come in) it takes an utterly ridiculous 6+ minutes! As I'm sure you can understand, this is completely unacceptable, and I'm still trying to find a decent answer to this.


The Code
So for now, let's just experiment with this:

SpotlightQuery1.Query = "kMDItemContentType = ""*audio*"""
SpotlightQuery1.Synchronous = true
SpotlightQuery1.Run

LBResults.DeleteAllRows
for i as Integer = SpotlightQuery1.Count - 1 DownTo 0
  LBResults.AddRow SpotlightQuery1.Item(i).File.Name
  LBResults.CellTag(LBResults.LastIndex, 0) = SpotlightQuery1.Item(i).File
next


Pretty short, no? Well, have fun waiting for those 22,000 results to show up. In the mean time, here's a cool tip:



Spotlight Results Window
Now check this out:

Sub Action()
  dim resultCode as Integer
  Declare Function HISearchWindowShow Lib "Carbon" (SearchStr as CFStringRef, Options as Integer) as Integer
  
  resultCode = HISearchWindowShow("bear", 0)
  if resultCode <> 0 then
    beep
  end if
End Sub

Eh? Eh?? How cool is that?! Essentially one line of code and magically your application has a REAL Spotlight window! I thought that was the coolest thing since sliced bread when I found it.


Finished
As always, you can download the project here.