Fixing Grammar Problems with Google Search (Intermediate AutoHotkey Tip)

The GooglePhraseFix AutoHotkey Script Corrects Many Common Spelling and Grammar Errors While Demonstrating How to Download Information from the Web…Without Opening a Browser

As a novel and practical AutoHotkey script worth stealing, I highlighted the GooglePhaseFix script written by aaston86 in Chapter Nine of the free e-book AutoHotkey Tricks You Ought To Do With Windows. (It originally appeared in the old AutoHotkey forum.) However, I didn’t take the time in the book to point out the important AutoHotkey tricks used for quickly downloading data from Web pages for immediate use or display. The GooglePhaseFix script offers some unique tools for accessing Web sites and extracting useful information.

After loading the script, the Hotkey action copies pre-selected text to the Windows clipboard, downloads the source code from a Google search for the clipboard contents, then runs a Regular Expression (RegEx) parsing the correction text from the page. Finally, the parsed result replaces the original selection.

If you type “Where is you’re house?”, highlight it, then hit the key combination CTRL+ALT+C, the sentence gets replaced with “Where is your house?”

The GooglePhraseFix.ahk script:

; Ctrl+Alt+c autocorrect selected text
clipback := ClipboardAll
Send ^c
ClipWait, 0
UrlDownloadToFile % "" . clipboard, temp
FileRead, contents, temp
FileDelete temp
if (RegExMatch(contents
, "(Showing results for|Did you mean:)</span>.*?>(.*?)</a>", match))
   StringReplace, clipboard, match2, <b><i>,, All
   StringReplace, clipboard, clipboard, </i></b>,, All
Send ^v
Sleep 500
clipboard := clipback

Note: The line of code containing the RegExMatch() function wraps using AutoHotkey line continuation techniques. AutoHotkey reads it as one continuous line.

After selecting the target text, activate the app by hitting the CTRL+ALT+C (^!c) key combination simultaneously.

Key Features of GooglePhraseFix

The GooglePhraseFix script offers a couple of useful tricks:

These two AutoHotkey operators make up the one-two punch for quickly obtaining data from specific Web pages—no slow browser loading or tedious Web searches. These commands build into the script everything needed to get the job.

ipfindIn the book, A Beginner’s Guide to Using Regular Expressions in AutoHotkey, I included a script for finding the worldwide location of any IP address by merely highlighting the address (or series of addresses anywhere in the page or document), then hitting the Hotkey combination. The location (or list of locations pops up in an informational window. While I don’t use the UrlDownloadToFile command, capturing data from the Web and the RegExMatch() function make up the backbone of the script.

Note: In the IPFind.ahk script, I use the ComObjCreate(“WinHttp.WinHttpRequest.5.1”) function to directly download the Web page source code to a variable. While this approach runs a little faster, the UrlDownloadToFile command used in the GooglePhaseFix.ahk script offers a less confusing implementation.

By applying these AutoHotkey tools to download data and run a Regular Expression, you could just as easily extract the number of calories in a food item or highlighted word definitions by accessing specific food or dictionary sites, respectively. This AutoHotkey script gets the job done much faster than loading a Web browser and doing individual searches.

The UrlDownloadToFile Command for Accessing the Web

The AutoHotkey UrlDownloadToFile command offers a simple tool for downloading specific Web pages. As the command syntax states, UrlDownloadToFile copies the designated page code to a file:

UrlDownloadToFile % "" . clipboard, temp

The % operator forces an expression from the embedded code. In this case, it includes the URL of the site ( with the Windows clipboard contents (clipboardconcatenated as the search term by placing the dot ( . ) operator between the two terms. AutoHotkey names the file temp placing it in the working directory.

Once the file resides on the computer, it can be manipulated in any way necessary. Commonly, a script reads the temporary file (temp) contents into a variable (contents) using the FileRead command:

FileRead, contents, temp

Since the UrlDownloadToFile command directly accesses the Web page, it executes faster than a browser.

The RegMatch() Function

The RegExMatch() function extracts the target information by identifying and locating key text structures in the Web page source code—pulling out the appropriate data.

Cover 200Understanding the details of how the RegExMatch() function works requires knowledge of Regular Expressions. This takes a bit more than a short explanation. (That’s why I wrote the book shown at right.) However, if you merely desire a glimpse into how Regular Expression work, check out this mini-tutorial on Regular Expressions.

While the GooglePhraseFix.ahk script may come in handy, it does not compete with the comprehensive solution found in the service from our latest software partner, Grammarly. AutoHotkey can handle autocorrecting commonly misspelled words, but the complexities of English language grammar require much more complicated techniques and algorithms than you might attempt with AutoHotkey. Fortunately, Grammarly proofreading and grammar checking software offers a free basic version.

One Last Tip: I would add the following to the GooglePhraseFix script just after the ClipWait 0 command:

If ErrorLevel
    MsgBox, No Text Selected!

That way AutoHotkey alerts you when you forget to select any text and the routine exits without executing the search download.



One thought on “Fixing Grammar Problems with Google Search (Intermediate AutoHotkey Tip)

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s