Object

SpotlightQuery Class

Used to perform Spotlight searches on Mac OS X 10.4 and above. It does nothing on earlier versions of Mac OS X and all other operating systems.

Events
Changed
Completed

Properties
Completed
Handle
Query
Synchronous

Methods
Count
Item
Pause
Resume
Run
Stop

# Notes

SpotlightQuery appears in the list of Built-in controls in the IDE, but since it is not subclassed from Control, you can instantiate it via code.

# Constructor

Name | Parameters | Description
--- | --- | ---
SpotlightQuery | [query as String] | The optional query parameter specifies the query. The default is no query.

An overview of the Spotlight API is at: http://developer.apple.com/documentation/Carbon/Conceptual/SpotlightQuery/index.html

Spotlight works by extracting metadata attributes from files on the user's hard disk. By default, this extraction is done in the background by Spotlight Importers. When an end-user does a Spotlight search, he is actually doing a search on the attributes that have been extracted via the importers. When you use the SpotlightQuery class, you must specify the attribute or attributes you are searching on using Spotlight keywords and syntax.

In other words, you will need to become familiar with Apple's MDQuery language. Each simple query is in the format of attribute=Value, where attribute is a Spotlight metadata attribute and Value is the target value.

Apple's list of searchable metadata attributes is at:

http://developer.apple.com/documentation/Carbon/Reference/MetadataAttributesRef/index.html#//apple_ref/doc/uid/TP40001689

For example, kMDItemContentType = "*audio*" would find all files that had a content type containing "audio" (case insensitive). The "*" is the wildcard character. You can combine expressions with "&&" (logical "And") and "||" (logical "Or"). For example, to find a file that was Audio and had a artist of Lifehouse, it would look like this: kMDItemContentType = "*audio*" && kMDItemArtist = "Lifehouse".

The complete description of MDQuery syntax is at:

http://developer.apple.com/documentation/Carbon/Conceptual/SpotlightQuery/index.html#//apple_ref/doc/uid/TP40001841

# Examples

The following synchronous query populates a ListBox with the list of audio files on the user's computer and the absolute path to each file. You can put the code in a PushButton.

Dim query as New SpotlightQuery("kMDItemContentType=""*audio*""")
query.Synchronous= True
query.Run
For i as Integer = 0 to query.Count-1
ListBox1.Addrow query.Item(i).File.DisplayName
ListBox1.Cell(i,1)=query.Item(i).File.AbsolutePath
Next
Exception err as SpotlightException
MsgBox "A Spotlight error occurred."

The following asynchronous query uses the search string that the user enters into an EditField and displays the filename and its absolute path in a ListBox. It uses a SpotlightQuery control named "Query" that has been added to the window.

First, add the following method "UpdateList" to the window:

ListBox1.DeleteAllRows
Query.Pause
For i as Integer = 0 to Query.Count-1
ListBox1.Addrow Query.Item(i).File.DisplayName
ListBox1.Cell(i,1)=Query.Item(i).File.AbsolutePath
Next
Query.Resume

In the SpotLightQuery's Changed and Completed event handlers, call the UpdateList method.

In a PushButton, enter the following code in its Action event handler.

If EditField1.Text <> "" Then
Query.Query ="kMDItemDisplayName = ""*"+EditField1.Text+"*"""
Query.Run
Else
MsgBox "Please enter a file name to search for."
End If
Exception err as SpotlightException
MsgBox "A Spotlight error occurred."

The following If statement checks to see if the user is running Mac OS X 10.4 or higher:

Dim sysversion as Integer
If System.Gestalt("sysv",sysversion) and sysversion >= &h1040 then
//you can call SpotlightQuery here
Else
//Don't bother calling SpotlightQuery
End if

# See Also

SpotlightException, SpotlightItem classes.