AppleScripting Google Desktop

Thursday, June 28, 2007 at 5:49 PM

Posted by Dave MacLachlan, Mac Software Engineer

Most people aren't aware that Google Desktop has a simple, but powerful, AppleScript interface. You can use this interface to write scripts that search with Google Desktop. The most basic script allows you to quickly search for some text:

tell application "Google Desktop"

search for "happy"

end tell


This will return an array of search results. A search result is a record containing the title, snippet, and URL of the result, along with other details. So if you wanted to display just the title of the first result, you could do this:

tell application "Google Desktop"

set results to search for "We wish"

set a to title of item 1 of results

end tell

display dialog a


Or if you wanted to open the first result:


tell application "Google Desktop"

set results to search for "you a"

set a to URL of item 1 of results

end tell

open location a


The "search for" command has several more powerful options, some of which are exclusively available in the scripting interface. Suppose you want to search for results between two dates:

tell application "Google Desktop"

set startDate to date "Monday, July 17, 2006 00:00:00 "

set endDate to current date

search for "happy" after date startDate before date endDate

end tell


Or, let's say you want to find only email results:


tell application "Google Desktop"

search for "birthday" restrict to email

end tell


Or, finally, you'd like to do an exact search (as opposed to a prefix search, which is the default):


tell application "Google Desktop"

search for "hoff" without prefix match

end tell

Of course, you can use all of this directly from the command line using osascript.


There are several other options for "search for" and a few other simple commands hidden within the dictionary, so poke around and see what you can do.