Look Up Words From Any Program

using AutoHotKey to look up words online

© 2016 by KV5R. All Rights Reserved. Rev 3/31/2016

Why

Have you ever wished you could select a word in any program and then look it up in any online search engine, encyclopedia, or dictionary? Well, you can!

What

AutoHotKey is a handy utility that allows you to define system-wide macros. It uses its own scripting language to carry out actions in response to hot-keys.

Download and install it.

How

Open a new text file (or your existing AutoHotKey.ahk file) in notepad or whatever text editor you like, and copy-paste the following script into it:

SendMode Input

; Ctrl-Shft-W (20160322 Send selected text to Wikipedia):
^+w::
{
Send, ^c
Sleep 50
Word := Trim(Clipboard)
Word := RegExReplace(Word, "[\s]+", "_")
Run, https://en.wikipedia.org/wiki/%Word%
Return
}

; Ctrl-Sh-D (20160322 Send selected text to dictionary):
^+d::
{
Send, ^c
Sleep 50
Word := Trim(Clipboard)
; Use next line with Wikimedia sites (convert spaces to _ ):
;Word := RegExReplace(Word, "[\s]+", "_")
;Run, http://localhost:8000/wiktionary_en_all_2015-11/A/%Word%.html
;Run, https://en.wiktionary.org/wiki/%Word%
; Others don't need space conversion.
Run, http://www.dictionary.com/browse/%Word%
;Run, http://www.finedictionary.com/%Word%.html
R;Run, http://wordnetweb.princeton.edu/perl/webwn?s=%Word%
eturn
}

; Ctrl-Sh-G (20160322 Send selected text to Google):
^+g::
{
Send, ^c
Sleep 50
Word := Trim(Clipboard)
Run, http://www.google.com/search?q=%Word%
Return
}

; Ctrl-Shft-A (20160322 Send selected text to Amazon):
^+a::
{
Send, ^c
Sleep 50
Word := Trim(Clipboard)
Word := RegExReplace(Word, "[\s]+", "+")
Run, http://www.amazon.com/s/?field-keywords=%word%
Return
}

; Ctrl-Shft-U (20160331 Send selected URL (http://blabla.bla) text to browser):
^+u::
{
Send, ^c
Sleep 50
Word := Trim(Clipboard)
Word := RegExReplace(Word, "[\s]+", "%20")
Run, %word%
Return
}

; PrintScreen ScrollLock PauseBreak to Cut Copy Paste (YAY!)
PrintScreen:: Send ^x
ScrollLock:: Send ^c
Pause:: Send ^v

; your other ahk scripts go here, before the final return.

Return

Save the file as AutoHotKey.ahk in your Documents folder (AHK expects to find it there).

You’ll want AutoHotKey to start with Windows (put a link to it in your Startup menu), and it’ll automatically load your script file if it’s located in your Documents folder and named AutoHotKey.ahk.

Start AutoHotKey.exe. It’ll simply make an icon in your system tray. To control it, right-click its icon and choose from menu. You can “Edit This Script” (opens it in Notepad), and after editing and saving, you must always remember to “Reload This Script.”

Using

Now you have five nifty keyboard shortcut macros that’ll do as the script says. Select a word in any open document, then:

  • Ctrl-Shft-W sends it to Wikipedia
  • Ctrl-Shft-D sends it to the dictionary of your choice
  • Ctrl-Shft-G sends it to Google
  • Ctrl-Shft-A sends it to Amazon
  • Ctrl-Shft-U sends any selected URL to browser

Note: These will override any previous definitions for these key combinations. Select your hot-keys carefully! For example, don’t use Ctrl-Shft-S because many programs use that as “Save As…”

I also included remapping of the PrintScreen, ScrollLock, PauseBreak keys to Cut, Copy, Paste — finally making those ancient ’70s teletype keys useful. You might want to print labels for them. Use a stick-back mailing label, print Cut Copy Paste on it, cover with clear packing tape, cut out, and put on those keys. Or just remember. If you don’t want those keys remapped (or don’t have them on your keyboard), just comment-out the lines in the .ahk file.

Selecting Text

You can drag-select a word, or double-click it. Note that double-clicking in some programs will also select the space before and/or after, so I added the Trim function to the scripts.

  • For dictionary lookups, you should select only ONE word; but if more than one, some dictionaries need conversion of space(s) to -, +, or _. If you send spaces in a URL string, the browser converts them to %20, which is the hex code for space.
  • For Wikipedia lookups, you can select one or more words, but it expects spaces to be converted to underscores, so the script does that for you.
  • For Google searches, it accepts the spaces, so no space conversion is needed (the browser converts spaces in URLs to %20). Note that if you send Google a single word, its listing will frequently start with a dictionary lookup. If not, you can force a dictionary lookup in Google by placing ‘define: ’ (without quotes) before the word.
  • That Amazon lookup is handy if you’re reading about some product and want to quickly have a look at it, see its price, read some product reviews, etc.
  • The URL lookup is handy for those times you’re reading along somewhere and run into an unlinked URL. This is common in many PDF documents and Kindle books, etc.

Formatting the String

When sending a string with more than one word, online resources handle the URL formatting differently. Looking at the above script, we see that RegexReplace can take the selected text and do a simple search/replace on it before sending the URL. To add another resource, you need to:

  1. Open a web browser and go to your resource.
  2. Do a search from within the resource.
  3. Look at the browser’s address bar to see how the resource formats its query string.
  4. Copy the address bar URL into your ahk script, then:
    1. Edit the RegexReplace line for conversion of space to the needed separator (but if the separator is %20, that’s a space, and no conversion is needed).
    2. Replace the search word in the URL string with the variable %Word%.
    3. Save the script, reload it, and try it.

In Closing

AutoHotKey can do many other useful things. Power-users have been using it for many years, saving billions of man-hours a few seconds at a time! Search online for autohotkey scripts and you’ll find a bunch.

I hope this is helpful! Now you can look up words from any program that allows select and copy. If you have an improvement or suggestion, please leave a comment below.

—KV5R

3 thoughts on “Look Up Words From Any Program

Leave a Reply

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