Spotlight and exact phrase searches: AppleScript application to include plural forms

Posted by Pierre Igot in: Macintosh
May 14th, 2010 • 4:27 pm

After I wrote about the fact that an exact phrase search (with quotation marks) in Spotlight fails to include plural forms in the search results, I discovered that it was possible to include simple plural forms (i.e. plurals with a simple final “s” appended to the singular) in exact phrase search results by using a raw query.

For example, if you want to search for the exact phrase “deep pressure technique” and you want your search results to include occurrences of both “deep pressure technique” and “deep pressure techniques,” you can use this raw query:

kMDItemTextContent=="deep pressure technique*"

You can also add a * wildcard before the phrase, in order to make sure that your results also include occurrences of the phrase that might be preceded by additional characters (like a contracted article in French, for example, as in “l’exemple recherché”).

My problem with raw queries in Spotlight is that they are rather tedious to use in the standard Mac OS X user interface for Spotlight. You have to open a search window in the Finder, manually add a search criterion using the “Raw Query” attribute, and then enter your raw query in a tiny text field where fewer than 50 characters are visible at any given time:

Raw Query in Finder window

Saving this search as a “saved search” or “smart folder” does not help much, because when you reopen a saved search, by default the search criteria are hidden, so you have to manually reveal them first before you can edit them:

Show Search Criteria

And even then, the “Raw Query” field is still tiny and still requires way too much horizontal scrolling with the cursor keys.

So I decided to try and find out if there was a way to design a better interface for this, using my main tools for customizing Mac OS X, i.e. a combination of AppleScript and Keyboard Maestro.

Unfortunately, it turned out to be a bit more complicated than I thought, because there is no obvious way to do a Spotlight search and display its results in a standard Spotlight search results window in the Finder using AppleScript commands.

You can use the do shell script command to run within an AppleScript script a shell script that performs a spotlight search with mdfind, but this will not return the search results in a standard Spotlight search results window in the Finder.

After some additional research and a plea for help on the AppleScript forum at Apple Discussions, I have ended up using the following solution:

-- replace the path in the first line by the path of your smart folder --> Raw query
set smartFolder to quoted form of "/Users/igot/Library/Saved Searches/ExactPhrase.savedSearch"

set a_text to text returned of (display dialog "Enter a phrase to search" default answer "")

if the text contains a double quote, sed replaces simple backlash by triple backlash,  \" --> \\\" 
if the text contains a & sed replaces & by & 
if a_text contains "\"" or a_text contains "&" then
	set a_text to do shell script "/bin/echo " & (quoted form of a_text) & " | /usr/bin/sed 's/\"/\\\\\"/g;s/&/&/g'"
end if

set cmd to quoted form of ("my $t = q(*" & a_text & "*);s/kMDItemTextContent==\".*\"/kMDItemTextContent==\"$t\"/g;")
do shell script "/usr/bin/perl -i -p -e " & cmd & space & smartFolder & " && open " & smartFolder

As you can see, this is a script that uses AppleScript to execute Perl commands. Now, I know nothing about Perl, so don’t expect me to explain all this to you. All I know is that it comes from a forum contributor named Jacques and that it works great. As far as I can tell, it needs an existing saved search file, which you can easily create by saving your initial search with the raw query (see above), and then it just modifies the saved search’s contents (a saved search is an XML file) and instructs Mac OS X to open the revised saved search as a Spotlight search results window, in order to display the results in the Finder.

In order to make this script accessible from anywhere, I used AppleScript Editor to save it as a stand-alone application, and then I defined a global macro in Keyboard Maestro to launch the application from anywhere using a keyboard shortcut.

The only additional problem I encountered is that, if the keyboard shortcut includes the Control key, and if I don’t release that Control key quickly enough when typing the shortcut, then Mac OS X interprets the process as launching a script application with the Control key down, which is the conventional shortcut for forcing the application to show a startup screen.

The only way to avoid this is to insert a pause at the beginning of the macro, or to use a shortcut that does not involve the Control key. I opted for the latter solution.

So now I have a global shortcut (I chose command-F19) that causes a dialog box to appear asking for the phrase I want to search for. If I type a phrase such as “deep pressure technique” (without the quotation marks, which are not required with this script), then the script opens a Spotlight search results window in the Finder that includes documents that match not just “deep pressure technique,” but also “deep pressure techniques” and other variations of the whole phrase.

This will probably be my preferred interface for launching Spotlight searches from now on. (I need exact phrase searches far more often than I need standard keyword searches.)

Comments are closed.