Make Google Dictionary Extension better with AutoHotkey

The other day I was doing assigned reading for my General Education Seminar. As a non-native English speaker, my vocabulary is not something that I’m proud of, and some big words in the reading started to slow me down. I turned to Google Dictionary for help, but the article was unfortunately in PDF format. I could not simply select the word and click the dictionary extension button. It just does not work with PDF file. I had to copy the word, hit the button, paste the word and then hit enter. Apparently, that was too much of a hassle for a lazy person like me, so I decided to write a script that allows me to use hotkeys to do the same task.

The extension does not work with PDF pages
The extension does not work with PDF pages

The script does few simple things. When the hotkey is pressed, it searches for the dictionary extension icon within the active window. If the icon is found, it continues to the next step, otherwise it makes a little beep to notify the user. Before it does anything, it first records the position of the mouse so that it can later return the mouse to its previous position. Then it copies the selected content, clicks the button found in previous step, sends ctrl+A to select all in the input field, and then paste the word and hit enter. Finally it returns the mouse to starting position so as not to distract the user.

A demo of my script working
A demo of my script working

It turned out to work pretty well, and saved me tons of time. As you can see, a simple press of ctrl+shift+z displays the dictionary entry of the selected word, which allows me to check the meaning of the word and continue reading without interruption. I guess this is probably not the most elegant solution, but for a half-an-hour dirty hack, it is good enough. According to xkcd “Is It Worth the Time?” chart, I’d say this project is a huge success.

Is It Worth the Time?
Is It Worth the Time?

That’s all for today. See you next time! Code and more information can also be found on my Github repo.

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

^+z::
WinGetActiveStats, winT, winW, winH, winX, winY
ImageSearch, IconX, IconY, 0, 0, winW, winH, *150 google_dict.png
if IconX {
    MouseGetPos , MouseX, MouseY
    Send ^c
    MouseMove, IconX, IconY, 0
    Click
    MouseMove, MouseX, MouseY, 0
    Sleep, 150
    Send ^a+{Enter}
    Send ^v+{Enter}
} else {
    SoundBeep, 1000
}
MouseGetPos , OutputVarX, OutputVarY
Return

;Esc::
;SoundBeep, 2000
;Reload

Creative Commons License Text published under CC BY-SA 4.0

Leave a Reply

Your email address will not be published. Required fields are marked *