SpotlightQuery

From Xojo Documentation

Class (inherits from Object)


Used to perform Spotlight searches on macOS. It does nothing on 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 fa-lock-32.png Query
Handle fa-lock-32.png Synchronous


Methods
Count Pause Run
Item Resume Stop


Constructors

Constructor(query as String)


Notes

Visit the Apple site for an overview of Spotlight: https://developer.apple.com/library/content/documentation/Carbon/Conceptual/MetadataIntro/Concepts/WhatIsSpotlight.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:

https://developer.apple.com/library/content/documentation/CoreServices/Reference/MetadataAttributesRef/MetadataAttrRef.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:

https://developer.apple.com/library/content/documentation/Carbon/Conceptual/SpotlightQuery/Concepts/Introduction.html

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

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.

Var 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.CellValueAt(ListBox1.LastAddedRowIndex, 1) = query.Item(i).File.NativePath
Next

Exception e As SpotlightException
MessageBox("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.RemoveAllRows
Query.Pause
For i As Integer = 0 To Query.Count - 1
ListBox1.AddRow(Query.Item(i).File.DisplayName)
ListBox1.CellValueAt(ListBox1.LastAddedRowIndex, 1) = Query.Item(i).File.NativePath
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.Value <> "" Then
Query.Query = "kMDItemDisplayName == ""*" + TextField1.Value + "*"""
Query.Run
Else
MessageBox("Please enter a file name to search for.")
End If

Exception e As SpotlightException
MessageBox("A Spotlight error occurred.")

See Also

SpotlightException, SpotlightItem classes.