Fixing AutoHotkey Web Lookup Scripts

If a Web Page Changes Format, the Data-Extracting Regular Expressions (RegEx) May Need Updating—Fixing the SynonymLookup.ahk Script

When writing a blog, I tend to use certain words over and over again. While rereading early versions, these redundant words jump out at me. Not only do they point out my limited vocabulary, but the repetitions tend to render my blogs a little more starchy and boring. That’s why I often resort to my always-loaded SynonymLookup.ahk script. This app saves time while making me look a little smarter.

The current version of SynonymLookup.ahk script lists more possibilities and marks antonyms (most of the time) with a caution sign (). (Click image for expanded view.)

After I discover a duplicated word, I highlight it, then hit the Ctrl+Alt+L Hotkey combination. A menu of possible replacements pops up. I click on the one that best fits my intent and the new term immediately displaces the original text. I habitually use this script.

When the SynonymLookup.ahk Script Breaks

Over the life of the script, I’ve encountered the menu shown at right a couple of times. This menu pops up whenever the script downloads and scans the source code 10 times without getting a RegEx hit—usually the result of code changes made by the source page Webmaster.

Note: If you see the above pop-up, click the App Broken! Check for Update? menu item to access the Free AutoHotkey Scripts page in search of a possible script update. Since I use SynonymLookup regularly, I usually notice the problem within a week or less and modify the RegEx—making it operational again. (If you discover the script not working and I haven’t posted an update, please contact me.)

Regular Expressions in AutoHotkey
Solve the mysteries of AutoHotkey Regular Expressions (RegEx).

For more information on how this script works, see “The SynonymLookup.ahk Web Data-Driven App Fails (AutoHotkey Adjustments).” To learn more about using Web pages to extract data for your AutoHotkey apps, see “Chapter Eight: A Simple Way to Find Out Where in the World That IP Address Is Located” of the book, A Beginner’s Guide to Using Regular Expressions in AutoHotkey

Fixing the Regular Expressions (RegEx)

When I run into this problem, I open the target Web page (click the bold word at the beginning of the menu) and access the source HTML code (right-click and select “View page source” in Google). Then I locate the primary list of synonyms—looking for a code pattern. In the current page, I found that the words “targetTerm” and “targetSlug” surround each possibility:

A section of the source HTML code for synonyms (and antonyms) of the term “word” found in the target Web page.

I merely needed to alter the previous RegEx search expression in the SynonymLookup.ahk script’s RegExMatch() function to include those boundary words :

SynPos := RegExMatch(SynPage,"""targetTerm"":""(.*?)"",""targetSlug""",Synonym,NewSynPos) 

This change made the script operational again. In fact, most to the menus now include even more possibilities than the previous version—although I do see the occasional odd placement of items on the menu. (I can’t take credit for the increase in synonym options. That’s purely a function of how The Webmasters rewrote the code.)

Note: To produce a lone double-quote (") for use in the RegExMatch() function and other expressions, escape double-quotation marks (") by preceding each with another double-quote ("").

Checking for Antonyms

I needed to change how the antonym check works. (The script places a yellow caution sign () before each antonym menu item—mostly.) Usually, the terms “Synonyms” or “Antonyms” appear somewhere between the old word location (SysPos) and the new word location (NewSynPos). For speed, I extracted that string and searched for the term “Antonym” and “Synonym”:

AntonymCheck := SubStr(SynPage, NewSynPos , SynPos-NewSynPos)
  If InStr(AntonymCheck,"Antonyms") {
	AntonymPos := 1
	}
  If InStr(AntonymCheck,"Synonyms") {
	AntonymPos := 0
	}

The script uses a true AntonymPos value to add the caution sign to likely antonym menu items:

If (AntonymPos = 1)
  Menu, Synonym, Icon, %Synonym1%, %A_Windir%\system32\SHELL32.dll, 78

This antonym check works pretty well—although occasionally, due to inconsistencies in the source code, it mislabels a word or two. I spent a little bit of time looking at those issues, but decided that the result offered enough information to get the job done without spending too much time perfecting it. If you need the precise information (with examples), clicking on the bold term at the beginning of the menu takes you to the Web page.

Click the Follow button at the top of the sidebar on the right of this page for e-mail notification of new blogs. (If you’re reading this on a tablet or your phone, then you must scroll all the way to the end of the blog—pass any comments—to find the Follow button.)

jack

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.)

Find my AutoHotkey books at ComputorEdge E-Books!

Find quick-start AutoHotkey classes at “Robotic Desktop Automation with AutoHotkey“!

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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