Build Your Own Dream Thesaurus Word Replacement Tool (AutoHotkey Web Application)

How to Use the Web to Feed Data to Your AutoHotkey Application—A Pop-up Menu to Replace Boring Words in Your Documents

Synonym Page
I highlight any word and hit the CTRL+L Hotkey combination. AutoHotkey downloads the code from the target Web page and parses the synonyms using RegEx—placing each in a pop-up menu. Click on any item and AutoHotkey replaces the selected word in any document or field open for text editing. In the example, the menu lists possible replacements for the word •PAGE—the first item in the menu. Click •PAGE to open at the target location.

I immediately added this short AutoHotkey script to my primary toolbox. It immediately provides me access to a list of alternative words in menu form (shown at right). Click on one of the entries and it instantly replaces the previously highlighted word. The apps beauty lies in the fact that I can utilize the Web for the database of synonyms. The script extracts the menu items directly from a Web page without opening my Web browser or processing any of the code—no ads. For writers and editors (or anyone who wants to expand their vocabulary), this one script provides enough incentive to plunge into regularly employing the free AutoHotkey Windows utility language.

If ever notices, I suppose this script may not make the owners of the site very happy. Any revenue they derive comes from the advertising. My app ignores all of it. They could change the formatting of the page, but then I would adjust the Regular Expression I use to extract the data. They might make an attempt to block my efforts, but I guess any such blocking technique would also block regular users. They could block my IP, but that would be a lot of work for just one person. (I would simply switch to another site offering synonyms.) In any case, I plan to continue using this AutoHotkey script until it stops working—for whatever reason. Then, I’ll fix it.

*          *          *

April 27, 2018

Did your SynonymLookup.ahk app stop working? changed their page formatting causing the script to fail, so I changed the Regular Expression in the latest version. I now use the links in the page source code—which needed some adjustments, so you may occasionally see a strange character.

Plus, you might also get antonyms, so I marked them with a caution icon (Antonyms caution). You’ll need to download the current script if you use a version older than this date.

I’ll write about the changes soon. I love this one! It’s better than ever!

April 30, 2018

Find the changes here: “The SynonymLookup.ahk Web Data-Driven App Fails (AutoHotkey Adjustments)

*          *          *

Note: To be fair, even when listing numerous possible synonyms, the script does not include all the opportunities for literary exploration found at The script does not differentiate between various definitions and only extracts equivalents from the first Web page. While SynonymLookup.ahk often displays over 100 alternatives, some words generate two or three Web pages on To pay homage and give credit to the site while offering the opportunity for further digging, the first Web page loads in your default browser when you click the bold keyword (e.g. •PAGE) at the top of the menu.

April 23, 2018 Update: I made some changes to the script which makes it more reliable. See “Waiting for Web Data to Download (AutoHotkey Quick Tip).”

Use the Web as Your Database

I recently offered an example of a database-driven AutoHotkey script,  “Write Less Code with Database Driven Apps (AutoHotkey Script Design).” This work great for relatively small applications with unique data, but building a database which might compete with the amount of information available on the Web presents an enormous challenge. Why reinvent the wheel when you can use data readily accessible on the Internet?

In my new AutoHotkey reference script (currently a work in progress), I access the online AutoHotkey documentation by directly downloading a Web page, extracting data with a RegEx, and displaying it in an informational MsgBox window. Writing an AutoHotkey script offers the advantage of directly downloading a Web page without processing all the code and embedded ads. It saves oodles of time.

Of course, I only get the Web page code, but that’s all I want. This Web page capture technique works on any Web page which means you can build a multitude of diverse applications driven by Web data. As a requirement, you need Internet access to make it happen, but these days almost everyone has that all of the time.

I wrote the new SynonymLookup.ahk to demonstrate an AutoHotkey script structure which takes advantage of data embedded in Web pages. It downloads synonyms from for replacement in any document or editing window.

A Universal Thesaurus App

Previously, when I found that I overused a particular word, I looked it up in a dictionary which offered a few alternatives. While useful, I found that the dictionary listed a limited number of options. Looking at the alternatives at, I found many more choices. While I had previously put together a short AutoHotkey script which opens the target page in my default Web browser:

  OldClipboard:= ClipboardAll
  Clipboard:= ""
  SendInput, ^c ;copies selected text
  ClipWait 0
  If ErrorLevel
    MsgBox, No Text Selected!
  Clipboard:= OldClipboard

I needed to copy-and-paste any acceptable word into my document. Between the loading of the Web page into my browser and the copying of my preferred term, the routine grew tedious. I decided to build the app SynonymLookup.ahk which creates a menu of synonyms from the words listed in the Web page, then, on a click, immediately inserts my selection.

Library Benefits

This replacement for the old SynonymLookup.ahk script uses a number of AutoHotkey tricks which you can apply to any AutoHotkey script which accesses a Web page for data, then creates a pop-up menu.

  • The script opens with the standard Clipboard routine where the user first selects a word in any text or editing field, then AutoHotkey uses the Clipboard contents to drive the action. (You can also use the app as a quick dictionary for any selected text. The menu entries offer alternative definitions in the form of synonyms.)
  • The GetWebPage(Web URL) function loads the source code text from the target Web into the variable SynPage for parsing and manipulating the code.
  • Three Menu commands turn the first menu item into a title and Web page link by making it the default (bold) entry and displaying a bullet.
  • The script loops (Loop command) through the Web page variable while extracting synonyms. The RegExMatch() function locates and pulls each synonym from the code based upon the unique HTML code surrounding the alternative replacement words. Aiding in the extraction of each synonym, the variable NewSynPos used in the RegExMatch() function increments the location of each synonym as AutoHotkey sweeps through the Web page code. By adding 1 to the found position (NewSynPos := SynPos+1), AutoHotkey the RegExMatch() function jumps to the next synonym.
  • For initiating new columns (every 20 items), the variable ItemCount increments after adding each new synonym to the menu. When the number of menu items reaches 20, the script starts a new menu column and resets the counter (ItemCount). This ensures even length columns.
  • The variable Synonym1 returned by the RegExMatch() function contains the first subpattern (the synonym). The variable SynonymList tracks each inserted synonym to prevent the script from attempting to add duplicates to the menu. AutoHotkey checks Synonym1 against the variable SynonymList for redundancy. If found, the Loop skips to the next iteration (Continue command). (No need to add it again.)
  • The script adds a menu item for each new synonym.
  • When AutoHotkey no longer finds a synonym (SynPos = 0), the script exits the loop using the Break command.
  • When chosen from the menu, the subroutine inserts the new synonym in place of the selected word. (Click outside the menu or hit ESC to cancel.)
  • When selecting a menu item or canceling, the script deletes the menu.

You can download or view the new annotated SynonymLookup.ahk script with the associated code at the Free AutoHotkey Scripts page. The following discussions cover the above features of the script as units—even though certain commands within each unit must appear either before the Loop starts, within the Loop, or after the Loop terminates. Check the annotated SynonymLookup.ahk script for the actual location of each command.

The Standard Clipboard Routine

Like many of my text manipulation routines, the SynonymLookup.ahk script uses the standard Clipboard routine. The user selects a word in any text or editing field, then AutoHotkey utilizes the Clipboard contents to drive the action.

  OldClipboard:= ClipboardAll
  Clipboard:= ""
  SendInput, ^c ;copies selected text
  ClipWait 0
  If ErrorLevel
    MsgBox, No Text Selected!
  ; [Enter routine code here!]
  Clipboard:= OldClipboard

The Hotkey combination CTRL+ALT+L initiates the action. If the user does not preselect any text, the routine returns the message “No Text Selected!”

Loading the Web Page Code into a Variable

The GetWebPage(Web URL) function loads source code text from the target Web page into the variable SynPage for parsing and manipulation.

SynPage := GetWebPage("" . Clipboard)

I used the following function (found in the examples on the UrlDownToFile command page) rather than the UrlDownloadToFile command because it loads the Web page code directly into a variable and, unlike the UrlDownToFile command, continues to work in AutoHotkey V2.0:

  whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
  whr.Open("GET", WebPage, true)
  RefSource := whr.ResponseText
  Return RefSource

This Web page capturing function uses the Windows ComObjCreate() function for accessing the Web page. This function works in a similar manner to other languages—such as Javascript for implementing Asynchronous Javascript + XML (AJAX) Web page access.

Note: You don’t need to understand how it operates. Just know that it works when you pass the URL of the target Web page as a parameter.

Creating a Title Entry for an AutoHotkey Menu

AutoHotkey menus do not offer a title option. I dealt with that by using the first entry as both a heading and link to load the target Web page in the default browser. This allows you to both check the proper search term and access to the Web page for further exploration of options.

The StringUpper command converts the lookup word to all caps for use in the menu heading. Three Menu commands turn the first entry into a title by changing the check mark to a bullet (+Radio), adding the bullet (check), bolding the entry (default), and linking the Web page with the LoadPage subroutine.

StringUpper, KeyWord, Clipboard
Menu, Synonym, add, %KeyWord%, LoadPage, +Radio
Menu, Synonym, check, %KeyWord%
Menu, Synonym, default, %KeyWord%

This menu header setup routine must appear before the Loop starts.

The LoadPage subroutine contains one line of code for loading the target Web page.


The LoadPage subroutine can appear almost anywhere in the script outside of the main Hotkey routine—usually toward the end of the file.

Extracting Synonyms from the Web Page Source Code

As it loops through the variable SynPage containing the Web page source code, the script extracts synonyms using the RegExMatch() function based upon the unique code surrounding the alternative replacement word, then adds a new menu item.

The variable Synonym1 contains the first subpattern (the synonym) returned by the RegExMatch() function.

NewSynPos := 1 ; Match extraction starting position
  ; RegExMatch() function wrapped using line continuation.
  SynPos := RegExMatch(SynPage,"(

The variable SynPos tracks the location of each synonym match as AutoHotkey sweeps through the Web page code. By incrementing the starting position by 1 (NewSynPos := SynPos+1) in the RegExMatch() function, AutoHotkey jumps to the next synonym.

Note: The most difficult part of writing this type of Web-based script involves the development of the Regular Expression which extracts the correct data. It requires studying the source code of the Web page to identify the text cues needed to locate the target data. I generally use Ryan’s RegEx Tester to form my RegEx code. For more information about Regular Expressions, see my book Beginner’s Guide to Using Regular Expressions in AutoHotkey.

Displaying the Menu

The Menu, Synonym, Show command displays the menu:

Menu, Synonym, Show

This command must appear after exiting the Loop.

Selecting a Synonym

When picked from the menu, the SynonymAction subroutine replaces the new term with the clicked word. (Click outside the menu or hit ESC to cancel.)

  SendInput %A_ThisMenuItem%{raw}

The SynonymAction subroutine called by every menu item (except the first title entry) uses the SendInput command to replace the selected keyword with the menu option. This subroutine should appear outside the Hotkey routine.

Creating Columns in a Menu

Some words return as many as 100 (or more) synonyms. This becomes cumbersome when the menu shoots off the screen. To create a shorter, wider menu, the variable ItemCount tracks the number of entries determining when to start a new column. After adding a synonym to the menu, ItemCount increments by 1. When ItemCount reaches 20, the script initiates a new column while it resets ItemCount to 1. This ensures a menu which fits on the screen.

Set ItemCount to 1 before the Loop starts:

ItemCount := 1

Inside the Loop, replace the Menu, Synonym, Add command with the following conditional:

If ItemCount = 20
  Menu, Synonym, Add, %Synonym1%, SynonymAction, +BarBreak
  ItemCount := 1   ; Resets ItemCount for new column.
  Menu, Synonym, Add, %Synonym1%, SynonymAction

When ItemCount reaches 20, the script starts a new column (+BarBreak option) and resets ItemCount to 1. Otherwise, after each new synonym addition, ItemCount increments by 1 (ItemCount++).

The Problem with Uneven Synonym List in the Menu

Initially, I found that many of the menus didn’t properly count the number of items in each menu column before starting a new one.

Synonym List Uneven
Since AutoHotkey doesn’t add redundant menu items, blank lines appear at the end of some of the columns.

I finally figured out that the Web page contains redundant synonyms which the Menu command won’t add to the menu—although the script counts each as an entry.

To fix this problem, I created the variable SynonymList to hold every inserted synonym. Outside the Loop, set the initial value of the variable:

SynonymList := Clipboard

Inside the Loop and before adding a new menu item, AutoHotkey checks SynonymList for redundancy. The script looks for the existence of the synonym:

If InStr(SynonymList,Synonym1)

If found, the Loop restarts (without adding the redundant word) by issuing the Continue command. (No need to add the word again. In fact, AutoHotkey won’t add the same menu item twice.)

As AutoHotkey embeds each new synonym into the menu, the script adds the term to SynonymList separated by a comma. Inside the Loop, just before or after adding the new menu item include the following line of code:

SynonymList := SynonymList . "," . Synonym1

Checking this list (SynonymList) before adding a new menu item prevents the script from attempting to plug duplicates into the menu thus ensuring the same number of options in each column.

Synonym List Even
By checking for redundant synonyms, the script adds the same number of items to each menu column.

Deleting the Menu

After use, the script deletes the menu. This eliminates the possibility that a new list of synonyms gets added to a previously invoked menu.

After menu selection or canceling, the script deletes the menu:

Menu, Synonym, DeleteAll

Restore the Old Clipboard

Before exiting the routine, restore the original contents of the Windows Clipboard:

Clipboard := OldClipboard

Enclose the Routine within a Hotkey Structure

Finally, turn the main routine into a Hotkey by enclosing it between the Hotkey combination and the Return command:



The Label subroutines, LoadPage and SynonymAction, and the GetWebPage(WebPage) function should appear outside the Hotkey routine—normally after.

A Map for Other Web Data Driven AutoHotkey Scripts

The SynonymLookup.ahk script does a ton of work for relatively little code. By using a similar structure, you can create any number of other Web data-driven AutoHotkey scripts. Remember, you can use something as simple as a MsgBox or a more complex GUI pop-up window for your output rather than the Menu structure shown in this script.


This post was proofread by Grammarly
(Any other mistakes are all mine.)

(Full disclosure: If you sign up for a free Grammarly account, I get 20¢. I use the spelling/grammar checking service all the time, but, then again, I write a lot more than most people. I recommend Grammarly because it works and it’s free.)


One thought on “Build Your Own Dream Thesaurus Word Replacement Tool (AutoHotkey Web Application)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s