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.
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:
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:
WinWaitActive, Libraries, , 2
MsgBox, WinWait timed out.
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:
... works. However ...
... 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.