SpotlightQuery

From Real Software Documentation

Jump to: navigation, search
Class (inherits from Object)

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


Events
Changed Completed


Properties
Completed Query
Handle Synchronous


Methods
Count Resume
Item Run
Pause Stop


Constructors

SpotlightQuery.Constructor(query as String)



Notes

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: kMDItemContentTree == 'public.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


See also Uniform Type Identifiers to see how to search files by type.


Caveat: Usually, the spotlight importer inside the IDE (a package called "REALbasic.mdimporter") is auto-detected by the Mac once the app gets launched for the first time. After a while (may be minutes or a few hours), Spotlight will start scanning the entire disk for project files (both in RbBF format, ending in ".rbp", as well as VCP format (".rbvcp" and related files) and make the results available to Spotlight.

If a project files gets saved in the IDE, it will be indexed immediately, though. This makes it possible to test if the importer works:

Launch the IDE, save any .rbp file, then issue "Get Info" in the Finder on this file. In the "More Info..." segment of the Info window, the classes of the project file should be listed then. If not, the importer is not functional.

If it turns out that the importer does not work, it may be that there's still an older importer present (e.g. the now-disfunctional REALbasic Spotlight importer released years ago). Removing this old importer should fix the issue.

To find any old importers, this is the best way to do it:

In Terminal, issue this command: "mdimport -L" This will list all Spotlight importers. One of them would be named "REALbasic.mdimporter". It will include the path (older) in which this importer resides. Only if it resides inside the new IDE's package, it's the right one. If others are found, they should be trashed. Repeat these steps until "mdimport -L" lists the importer inside the IDE.

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("kMDItemContentTypeTree == 'public.audio'")
query.Synchronous=True
query.Run
For i as Integer = 0 to query.Count-1
ListBox1.Addrow query.Item(i).File.DisplayName
ListBox1.Cell(ListBox1.LastIndex,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 a TextField 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:

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


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 TextField1.Text <> "" Then
Query.Query ="kMDItemDisplayName == ""*"+TextField1.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.

Personal tools