Checking Your Internet Connection, Plus a Twist on a Secret Windows Feature (AutoHotkey Quick Tips)

If Your AutoHotkey Script Depends Upon Internet Access, Check for an Active Connection Before Continuing, Plus a Surprising Trick for Accessing Hidden Windows Features

Seven Book AutoHotkey AutoHotkey Library Deal!I’ve written a number of scripts which access the Internet for data: IPFind.ahk for locating where in the world an IP address resides; RhymeMenu.ahk for popping up a list of rhymes for any selected word; SynonymLookup for replacing boring words (the impetus for this blog); AutoHotkey Quick Reference, both the now-defunct AutoHotkey reference tool and the new AutoHotkey reference tool currently under development require the Internet; and (not by me) the GooglePhraseFix script posted on the AutoHotkey forum by aaston86; plus, any script which attempts to launch a Web page. All of these apps require an Internet connection to work.

If you attempt to access the Web without an Internet connection, AutoHotkey throws an error page (shown below). Rather than allowing this type of disconcerting window to make an appearance, wouldn’t it be better to trap the error in advance?

Internet Error
Eventually, AutoHotkey tells you when you don’t have Internet access—in a way.

Checking for an Internet Connection

What if while processing the SynonymLookup.ahk script (or any other Web-based app), AutoHotkey tells you when it finds no Internet connection? Naturally, when no coupling with cyberspace exists, the script fails—which (based on the last blog “Waiting for Web Data to Download (AutoHotkey Quick Tip)“) could conceivably create an infinite loop. (In reality, the attempt to download displays an error as shown above giving the user an opportunity to cancel.) Notifying users in advance that they have no Internet connection works best:

If (ConnectedToInternet() = 0)
  MsgBox Internet not connected!

The ConnectedToInternet() function for testing the existence of an Internet connection uses the DllCall() function:

  Return DllCall("Wininet.dll\InternetGetConnectedState"
        , "Str", flag,"Int",0)

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

It’s that simple! You can find this handy Internet-check function at a number of places on the AutoHotkey forums. As with most DllCall() statements that I run across while searching for AutoHotkey solutions, I don’t make any attempt to decipher each portion of the function parameters. If it works, I’m happy! Anytime you need to check for Internet availability, include the above two snippets of code.

Running Network Troubleshooter

After AutoHotkey tells you that you don’t have Internet, suppose you want to run Windows Network Troubleshooter (or, for that matter, any other Windows Control Panel tool)? Normally, you need to search either through the Start Menu or Control Panel to locate the right tool for correcting a problem. You’ll find here a cool little trick for running any of the Windows utilities directly from AutoHotkey—no searching required.

Ideally, you set up a shortcut directly opening you target Windows tool. However, finding the path to a particular utility can present a challenge. The method I offer here gives you a technique for creating a shortcut for virtually any Windows settings gadget—plus you don’t need to know either the name of the tool or its path.

It all starts with the Secret Windows Tool Mode.

Use the Secret Windows Tools Mode to Create Shortcuts

In a previous blog, I demonstrated how to secure access to the hundreds of Windows utilities by opening the hidden tools folder. (See, “Add Secret Windows Tools (God Mode) to QuickLinks Menu (AutoHotkey Tip).”) All the links display plain English statements about each tool’s action. This makes it easy to locate features without digging through a multitude of other windows—such as categories within the Control Panel.

Even better, you can turn any of the actions into a shortcut taking you directly to the desired tool—including the correct tab within a window. (I know of no other way to do this, but I find this approach so easy that I haven’t taken the time to look.)

I opened the Windows Tools mode and use the search term “Internet” to filter the topics. The results appear in the image below:

Internet Connection Tools

I right-clicked on the action statement and selected Create shortcut from the pop-up menu. While Windows would not allow me to place the shortcut in this secret folder, it did let me place it on the Desktop.

Note: Who knew? To be perfectly honest, I had no idea that this would work. I was merely searching for some way to load the Network Settings window using AutoHotkey. I habitually right-click on links to check out the offerings. I was as surprised as anyone that it allowed me to create a shortcut. I immediately realized that by creating key shortcuts, I could add direct access (no rummaging through Windows) for most critical Windows tools to any AutoHotkey script.

Internet Not ConnectedThe shortcut on the Windows Desktop used the action statement as its name (i.e. Find and fix networking and connection problems – Shortcut). Double-clicking the shortcut loads the network troubleshooting program. I changed the name to Network Troubleshooter.

Now I can add the option to my AutoHotkey routine checking for Internet availability:

If (ConnectedToInternet() = 0)
  MsgBox, 4, , Internet not connected! `r `rRun diagnotics?
    IfMsgBox Yes
      Run, %A_Desktop%\Network Troubleshooter.lnk

This MsgBox gives the option to click Yes and load the Network Troubleshooter.

I recently added the shortcut to my QuickLinks folder:

QuickLinks Network Troubleshooter
I added the new Network Troubleshooter Tool shortcut to my QuickLinks/Tools folder to create an instant access point to the Windows utility.

This Windows shortcut technique opens up numerous possibility for making it easier to get around your computer with AutoHotkey.


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

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 )

Facebook photo

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

Connecting to %s