Waiting for Web Data to Download (AutoHotkey Quick Tip)

A Look at a Manufactured Looping Technique Using the Goto Command to Ensure the Download of Web Page Source Code in an AutoHotkey Script

EatCheeseBurgerCartoonI ran into a problem with the SynonymLookup.ahk script. On occasion, the menu would appear showing only the original bold and bulleted search term as its sole menu item. This occurred when the script finished processing before downloading the source code from the Thesaurus.com page. As often happens when working on the Internet, the Web connection took a little too long to perform its job.

A common headache with any AutoHotkey script which uses the Web, the time it takes to interact with a site and download its content profoundly fluctuates. In the case of the SynonymLookup.ahk script, I needed to ensure that the variable containing the Web page source code existed and contained text before continuing to parse the synonyms.

The idea of checking the variable’s content before continuing the script processing sparked my initial approach but I later found a more reliable technique for identifying a completed source code download. In the next few blogs, I plan to offer the following tips:

  1. How to ensure a complete download when using Web page source code to drive an AutoHotkey app. (No browser in the mix.)
  2. How to check for Internet access when running an AutoHotkey script.
  3. How other AutoHotkey users have attempted to determine when a Web page fully loads in a browser before continuing an AutoHotkey script—usually for auto-login scripts.

Library Benefits

Note: Originally, I included all of the above topics in one blog. However, I felt that important AutoHotkey techniques lost emphasis when combined into one long article. Therefore, I’ve broken the original piece into three parts for readers to ponder individually. (I plan to return in short order to the data table-driven legal term script from the last time.)

First, I deal with the source code download issue which occurred in my SyonymLookup.ahk script. For more details about the script and how it works, see “Build Your Own Dream Thesaurus Word Replacement Tool (AutoHotkey Web Application)” and “Free Windows App for Instant Synonym Replacement Menu.”

Checking for Web Page Source Code Download

While I plan an overview of the efforts to detect loaded Web pages in browsers, none of those techniques deals with my problem in SynoymLookup.ahk. This script completely bypasses browsers and directly downloads the code into an AutoHotkey variable. However, even though it uses no browser to slow things down, connecting and downloading from a remote Web server remains a fickled procedure.

I found that a delay in the source code download could cause the pop-up menu to display only the first title listing with no synonyms. If I ran the Hotkey again, it usually worked—but not reliably.

I tested a number of the standard tricks (plus a few non-standard ones), not unlike the ones used by people loading Web pages into browsers (topic number three). I inserted the Sleep command, checked for a blank variable, and tried out a few other options including messing with the whr.Open(“GET”, WebPage, true) function. While with many of my attempts the reliability appeared to increase, none of those techniques completely eliminated the problem. Irregularly, the menu continued popping up with no synonyms.

When a Web Page Doesn’t Download, Try It Again!

Occasionally, everyone experiences a Web page hanging in limbo in the middle of a load. Clicking the Reload icon offers the only reliable method for breaking the stalemate. While this technique doesn’t extend much of a solution for automatically loading Web pages into a browser in AutoHotkey scrips, it can work well for the direct download of source code.

Rather than rerunning the Hotkey manually, I only needed to force the script to immediately retry the download when the menu failed to load any synonyms. I added the following elements to the SynonymLookup.ahk script:

  1. A Label name to mark the restart point in the script when the download fails. In this case, at the spot just before AutoHotkey attempts to download the source code.
  2. An If conditional to detect and trap a failed download.
  3. A Goto, Label command which jumps back to the Label name restart point.

I call this technique a manufactured or fabricated loop because it creates the repeating condition without employing either the Loop or While command. You can initiate the same reiterative action with either of those built-in commands, but it would add slightly more complication (i.e. curly brackets {…}) than the simple Goto statement.

First, we add the Label name to the appropriate spot in the script:

MenuReload:

SynonymList := Clipboard 
SynPage := GetWebPage("http://www.thesaurus.com/browse/" . Clipboard)

Remember, a Label name merely acts as a marker (or signpost) in the script and has no effect on the processing. Unless called by another command, AutoHotkey ignores Label names. (In the book Beginning Tips for Writing AutoHotkey Scripts, I go into detail about the use of Label names in combination with the GoTo and GoSub commands.)

Second, we determine a method for detecting script failure. In our situation, when the Web page source code doesn’t download, only one item appears in the menu—the bold search key header. While I didn’t find a built-in variable in AutoHotkey which contains the menu length, I did uncover a DllCall() function which returns the number of options in a menu. From the examples in the online documentation for the MenuGetHandle() function:

Menu_item_count := DllCall("GetMenuItemCount", "ptr"
          , MenuGetHandle("Synonym"))
 
If Menu_item_count = 1
  GoTo, MenuReload
Else
  Menu, Synonym, Show

(The DllCall() statement (in red) word wraps using AutoHotkey line continuation techniques for display purposes in this blog.  AutoHotkey reads the broken string as one continuous line.)

When only one item appears in the menu, the download has failed. Thus, the script jumps back to the MenuReload point. Otherwise, the script displays the menu. This fabricates a loop which continues retrying the Web connection until the script actually downloads the source code text and inserts the synonyms into the menu. It works like a dream!

While not necessarily a useful technique for determining when an entire Web page completely loads into a browser, I recommend it for apps which depend upon downloading source code from the Web.

Next time, a quick tip on detecting an Internet connection in AutoHotkey.

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

Advertisements

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 )

w

Connecting to %s