Dictionary lookup on Snow Leopard with non cocoa apps

Hi there, I was wondering if you could help me with some applescript. For the last 6 hours I’ve been trying to learn and code my own script, but am now exhausted (total n00b).

Basically I’m running Snow Leopard, and want to do quick dictionary lookup with the trackpad in non-cocoa applications (like firefox). I doubt I can do this with mac’s inbuilt dictionary lookup (I’ve tried with the shortcut key and it only works with cocoa apps), so I just want to copy and paste the word into the dictionary app automatically.

My process/workflow of thought is this:

  1. Mouse Cursor Hovering over word I want to lookup
  2. Double Click on current word to highlight it (apple scripted)
  3. Copy to Clipboard
  4. Open Dictionary App
  5. Paste word and enter

Now with examples over the internet I can do the second part to paste from clipboard and run dictionary, here is my script so far:

tell application “Dictionary” to activate
tell application “System Events”
tell process “Dictionary”
tell menu bar 1
tell menu bar item “Edit”
tell menu “Edit”
click menu item “Paste”
tell (key code 36)
end tell
end tell
end tell

	end tell
end tell

end tell

And I can set the three finger tap gesture using MultiTouchTool to run the script.

I just need help scripting a double click where the mouse is hovering, so it can highlight the word and then copy it to clipboard.

Is there anyway you could help me with this?

From what I’ve gathered, this is quite hard to do as a universal thing, and requires specific gui scripting. I tried using xtools, couldn’t get it to work. But I’d like to make it as universal as possible, rather than specific applications.

Any help would be much appreciated guys, I’m really stuck on this.

Regards,

manny

Hi, manny. Welcome to MacScripter.

There’s actually a much simpler way to do this, although I haven’t seen it bandied about much:

open location "dict:///" & (the clipboard)

I’m not sure it’s scriptable. Wouldn’t it be simpler just to double-click manually?

Cheers buddy, here’s my script I came up with today that works:

Script 1:
on run
set mouseToolsPath to (path to home folder as text) & “Desktop:MouseTools”
do shell script quoted form of POSIX path of mouseToolsPath & " -doubleLeftClick"
do shell script quoted form of POSIX path of mouseToolsPath & " -doubleLeftClick"
tell application “System Events”
set theProcess to every process

	tell process theProcess
		
		keystroke "c" using {command down}
		
	end tell
end tell

end run

SCRIPT 2:
tell application “Dictionary” to activate
tell application “System Events”
keystroke “v” using {command down}
tell (key code 36)
end tell
end tell

I’m using automator so there’s two separate scripts. Works quite well.

I’m going to try and implement your script now. :slight_smile:

I had to add another double click (6 clicks now) because when the dictionary app was open, sometimes it wasn’t selecting the word in the other program (firefox) correctly.

I also added a delay, otherwise it would sometimes go on the previous clipboard word (only when dictionary app was already open.

And replaced my paste code with the simple dictionary code Nigel gave me. :slight_smile:

I still however have to use 2 scripts. Because delay seems to effect the whole code wherever I put it, when I only want it to effect the pasting part of the code.

SCRIPT 1:
on run
set mouseToolsPath to (path to home folder as text) & “Desktop:MouseTools”
do shell script quoted form of POSIX path of mouseToolsPath & " -doubleLeftClick"
do shell script quoted form of POSIX path of mouseToolsPath & " -doubleLeftClick"
do shell script quoted form of POSIX path of mouseToolsPath & " -doubleLeftClick"

tell application "System Events"
	set theProcess to every process
	
	tell process theProcess
		keystroke "c" using {command down}
	end tell
end tell

end run

SCRIPT 2:
delay 0.5
open location “dict:///” & (the clipboard)

Ok now this is mastered so a three finger tap will bring up the definition in the dictionary on any app where you can highlight the word with a double click, we need to take this one step further. We need to integrate the word lookup feature in any app.

Being a complete n00b, I’d really appreciate all the input I can get, as in to help me overcome the “obvious” things to most people.

Here’s my train of thought:

CMD + CTRL + D brings up word lookup for cocoa apps only.

From what I’ve read, dictionary.app can’t be apple scripted. So I’m guessing copying the word from clipboard is out of the question? Please correct me if I’m wrong, because this solution seems the easiest.

So my other train of thought is this. Can we make a small terminal cocoa app in applescript that would support the dictionary lookup function, copy the word into there with clipboard, then position the result using co-ordinates from the mouse cursor?

Any thoughts guys?

Also do all cocoa apps with text support dictionary lookup?

Actually looking at the word lookup on Snow Leopard, it looks tacky, too small, and not nice in general. While the actual dictionary app looks much better, font is clearer, and nicely paragraphed. So I won’t be pursing this project any further in that sense.

However I would like to improve my app just a tiny bit more.

  1. Is there anyway I can shorten the gap between the loading of the app and when the gestures made?
  2. Can I make it a stand-alone app that will work on any computer, rather than having to use MouseTools library each time?

For speed it goes something like this:

  1. Gesture
  2. Loads Application (Takes 5 Seconds+)

Basically I want to integrate it so it runs part of the operating system, or in the background constantly so it can be accessed faster, rather than having to load and exit the script every time.

The reason being it doesn’t feel smooth. With a click you get it in less then 1 second. I want to aim for at least 1-2 seconds. Also the problem being is if I move my cursor after activating it (within the 5 seconds), it won’t work correctly.

Any help guys, is it possible to run this application as part of the OS, and hide it from doc view also?

And is it possible to integrate the mouse tools, so I can make it a stand alone app?