Displaying the Finder’s Spotlight Search Window

Applications can provide users direct interaction with Spotlight by displaying the standard Finder search interface.

The NSWorkspace method showSearchResultsForQueryString: provides a simple interface to the Finder search window. This is the programmatic equivalent of the user switching to Finder, creating a new window, and typing the search string into the search field.

The code fragment in “Displaying the Finder’s Spotlight Search Window” demonstrates extracting a string value and displaying the search interface.

Listing 4-1  Displaying the Finder Search Window

resultCode=[[NSWorkspace sharedWorkspace] showSearchResultsForQueryString:[sender stringValue]];
 
if (resultCode == NO) {
    // failed to open the panel
    // present an error to the user
    }