AutoHotKey: Never Use 20 Keys When You Can Use 2

Have you ever wondered why you have to go through the same complex process of keystrokes over and over? Have you ever accidentally hit a useless key and wondered why that thing even existed? Have you ever wanted to be able to automate tasks on your PC, draw simple User Interfaces, or create conditional key mappings with a single, tiny tool set? If so, the folks over at AutoHotKey might be able to help you out.

AutoHotKey is a free, open-source utility for Windows. At its core, it is a very small code interpreter with the ability to convert a simple library of key words into instructions for your PC. What that can mean, in practice, is hard to describe without concrete examples, so let me give you a few.

To get started with AHK, go to the website and download the latest installer. Once you've done that, AHK will be doing absolutely nothing. It won't even be monitoring in the background. One of its key goals is to use the minimum possible resources, so it does nothing unless it is explicitly told to do so. To get it started, I created a blank script, just an empty text document called "startup.ahk" and put it in my PCs startup folder. Once you boot up, or manually kick it off, it will very quietly run in the background doing whatever you told it to do.

At its simplest, AHK allows you to override key input with your own macros. To start off, let's look at the "Insert" key. Almost every time I have hit that key, it has been by accident. It will toggle the default "Insert" typing mode into "Overwrite", which I never want. So, let's defang that key, and make it useful at the same time. Paste this code into startup.ahk script.

; Append selection to clipboard
insert::
bak = %clipboard%
Send, ^c
clipboard = %bak%`r`n`r`n%clipboard%
return

In this code, insert here names a key, and the double-colons indicate a shortcut. Once the script is restarted, this function will override the OS's default insert function. The contents of the "clipboard" built-in variable wil be copied to the "bak" variable. Then, the normal CTRL + C copy command is sent to the keyboard. Finally, the new contents of the clipboard is appended to the old and posted to the system clipboard. Now, instead of overwriting the clipboard every time, you can keep the old value, which might be useful if you are doing some complex text restructuring. And it keeps the Insert key from doing things you don't want.

You can also use AHK to launch applications and have them interact with the rest of the OS in new ways. For instance, try adding this to your script:

; Search highlighted term in Google
#G::
bak = %clipboard%
Send, ^c
Run, http://www.google.com/search?q=%Clipboard%
clipboard = %bak%
Return

So, assume you are reading a document written by someone with a better vocabulary than you. With this function, you could highlight the perplexing term and hit WIN + G. This would copy the word to your clipboard, then launch a Google search in your default browser, with that word already queried. And, as an added bonus, the previous contents of your clipboard are buffered and returned to the original state.

AHK can be used to speed up a complex process and make it an easy one. For instance, I find I often get this annoying message when I try to delete a folder in Windows 7:

"Folder In Use:The action can't be completed because the folder or a file in it is open in another program"

This can be tied to a feature of Windows Explorer that displays files as thumbnails rather than icons. This is a useful feature, particularly when looking for a certain media file, but the unfortunate side effect has forced me to leave it turned off. You can toggle it by going through this process:

Windows Explorer->Organize->Folder and search options, View tab -> Always show icons, never thumbnails

But what if, instead of navigating through a complex menu maze every time I wanted to change it, I could just use a quick shortcut? Try this script:

;Toggle Icon view to release locked folders and files
#T:
Run %windir%\explorer.exe
WinActivate, Libraries
WinWaitActive, Libraries, , 2
if ErrorLevel
{
    MsgBox, WinWait timed out.
    return
}
else
     Send !t
     Send o
     Send +{Tab}
     Send {Right}
     Sleep, 50
     Send {Tab}
     Sleep, 50
     Send {Tab}
     Sleep, 50
     Send {Tab}
     Sleep, 50
     Send {Down}
     Sleep, 50
     Send {Space}
     Sleep, 50
     Send {Tab}
     Sleep, 50
     Send {Tab}
     Sleep, 50
     Send {Enter}
     Send !{F4}
return

Hit WIN + T, and Explorer pops up. Over the next couple seconds, moving slightly faster than you eyes can follow, the script checks to make sure the window opened, then uses ALT + T to start navigating through the tools menu. It pauses the barest micro second between each key stroke, just to make sure that the OS launches the needed menu, before moving on to the next step. At the end, the script hits ALT + F4 to close Explorer, and you are back where you were, only now your Thumbnails setting has been toggled. Hit WIN + T again to set it back to whatever it was before.

And that really only begins to scratch the surface of what this tool can do. This is one of those tools limited only by one's imagination. That said, there are a couple caveats. For starters, the C-based syntax will throw you a loop every once in a while by not acting like you'd expect. For instance:

if(condition){
   action
}

... works. However ...

if(condition)
{
   action
}

... does not. The syntax is rather particular in that way.

And, word of caution: avoid using the Shift key in scripts. It can grab capital letters and loop maniacally. Just saying.

Regardless, it's amazingly useful. Look to see more scripts like these in future posts.

Comments

AutoHotKey is superb, one of my favorite programs of all time. Nice intro on it Dan -- I found this article because I was having the same frustrating problem with accidentally hitting the Insert key and going into Overwrite mode (has anyone ever used that mode on purpose since, say, 1995?)

Another powerful AHK feature that many casual users may find very useful is the text replacement. I used to use this all the time when I was an IT help desk technician to quickly and easily download utilities while remotely connected via VNC into users' computers, without needing to remember or copy+paste the long download URLs.

The basic format that you would put in your .AHK script is:

::shorthand::text you want to replace "shorthand" with

So for example if you wanted to pop in the URL to download Malwarebytes Anti-Malware from MajorGeek's website, you could use the following text replacement macro:

::mbam!!::http://majorgeeks.com/download.php?det=5756

Then whenever you type mbam!! and hit Tab/Enter/Space, AutoHotKey will replace it with the full URL. Note that I used two exclamation points so that it would not replace "mbam" when I type it for other reasons. I could've used anything though, such as "mbam$" or "dlmbam."

~Tony

Hi, Tony. Thanks for the comments. They made me realize that I could have made this even simpler. If you just wanted to make the insert key stop doing anything, you could do something like this:

; Disable Insert
insert::
return

That's an even simpler way to do it.

Hi Dan ,

if i have to use the ahk code for google reverese image search / where i enter the image url - how do i modify the ahk script...

i tried doing
Run, http://www.image.google.com/search?tbs=Sbi:%Clipboard%
but it didnt work

regards
Sachit

The "GET" method (all info in the URL) for Google searches can only work with textual information. Also, I'm pretty sure AHK pulls only text from the clipboard.