Send highlighted words/phrases to Google Desktop

May 15, '07 07:30:00AM

Contributed by: roncross@cox.net

I'm sure by now that you've had the chance to download Google Desktop (GD) and compare it to Spotlight. I am also sure that most people that have used GD can note many improvements, such as the ability to activate GD by highlighting a word or phrase in any application and then using a contextual menu or hotkey to bring up GD to perform a search and return the results.

Well, since I am not yet able to put it into a contextual menu, I have chosen to use Butler to set up a smart item keystroke -- along with writing an AppleScript -- that will allow me to select a word or phrase from any application and then send it to GD for matching results. In order for the script to work, you must check 'Enable access for assistive devices' in the Universal Access System Preferences panel. I am sure it can also be done with other browsers, but I choose Safari (default browser) in this case.

First, I set up a container in Butler and then added a Keystrokes Smart Item to it. I gave the container a hotkey (Control-G in my case). For the Keys section of the Keystrokes Smart Item, I entered Command-C (by typing an actual Command-C). Second, I created the following script, saved it as an application, and placed it inside the container -- underneath the Keystrokes Smart Item I created earlier.

Here is the code:

tell application "Google Desktop"
  open desktop homepage
  delay 3
end tell
tell application "Safari"
  activate
  set this_search to "document.forms[0].elements[0].value="
  set escquotes to """
  set thevalue to "'" & escquotes & (the clipboard) & escquotes & "'"
  set mysearch to this_search & thevalue
  do JavaScript mysearch in document 1
end tell
tell application "System Events"
  delay 1
  keystroke return
end tell
Third, I clicked on the container I just created and in the Hot Key section, I chose Launches all children sequentially from the drop down menu. That's pretty much it.

You might have to play around with the delay times in the code to optimize the performance of the script, depending on the machine you are using and your typical CPU load. By default, the script will search for a phrase if you highlight more than one word; otherwise, it will search for a word.

If you wish to search multiple words instead of the phrase, simply remove the [escquotes] bits from the script. Alternatively, you could remove the quotes after the initial results are returned, or simply modify the script to handle both cases using a dialog box. You could also just create another script without the [escquotes], so that you could handle both cases. There are many ways to do this to suit your needs.

Now, any time I want to send a highlighted word or phrase to GD, I just simply invoke the script via a hotkey and I'm done.

[robg adds: I haven't tested this one...]

Comments (5)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20070512181449390