GUI Scripting the Dictionary (Leopard)

I can get this to work sporadically when running it from Script Debugger 4, and not at all from the Script Editor. Anyone see what I’m screwing up here?

--set theWord to the text returned of (display dialog "What word would you like to look up?" default answer "")
set theWord to "hasty"
set R to {Dict:missing value, Thes:missing value}
open location "dict:/// " & theWord
activate application "Dictionary"
delay 1
-- Dictionary remembers whether it was Dictionary or Thesaurus, set it to Dictionary.
tell application "System Events" to tell process "Dictionary" to tell window 1 to tell checkbox 2 to ((perform action) click)
delay 1
set R's Dict to paragraphs of copyContents(1)
delay 1
-- This gets us the Thesaurus window
tell application "System Events" to tell process "Dictionary" to tell window 1 to tell checkbox 3 to ((perform action) click)
delay 1
set R's Thes to paragraphs of copyContents(2)

on copyContents(n)
	try
		tell application "Dictionary"
			activate
		end tell
		delay 0.5
		tell application "System Events" to tell process "Dictionary" to tell window 1 to tell splitter group 1 to tell scroll area 1 to tell UI element 1 to tell group n to tell scroll area 1 to tell UI element 1 to set focused to true
		delay 0.5
		my do_menu("Dictionary", "Edit", "Select All")
		my do_menu("Dictionary", "Edit", "Copy")
		return the clipboard
	on error E
		return E
	end try
end copyContents

on do_menu(app_name, menu_name, menu_item)
	try
		-- bring the target application to the front
		tell application app_name
			activate
		end tell
		tell application "System Events" to tell process app_name to tell menu bar 1 to tell menu bar item menu_name to tell menu menu_name to click menu item menu_item
		return true
	on error error_message
		return false
	end try
end do_menu

Hi Adam. I looked at this and I use script editor. It didn’t work which of course you know. So I focussed on getting the open location dict command to work, because that didn’t even work for me. 2 things I noticed about the dictionary. 1) it isn’t scriptable and 2) it was taking unusually long to perform a search. Here’s how I fixed both things…

  1. I could tell application “Safari” to open location dict://
  2. the long search times were due to preferences in the dicionary application. My dictionary was by default set to search wikipedia and apples website, and it was those internet searches that slowed up the script.

So tell Safari and uncheck those prefs and the open location dict command worked perfectly with script editor.

FYI, this automatically activates Dictionary with any problems for me:

open location "dict:///example"

Edit: I just came across this: http://www.tuaw.com/2007/03/05/monday-man-page-curl/

That’s rather neat, Bruce:

set tWord to text returned of (display dialog "Please enter a single word" default answer "")
try
	set Def to do shell script "curl dict://dict.org/d:" & tWord
on error
	display dialog "Not found"
	return
end try
set O1 to offset of "151" in Def
set O2 to offset of "." & return & "250 ok" in Def
set tDef to text (O1 + 4) thru (O2 - 2) of Def
tDef

Now if there was a way to access to access the thesaurus too.