January 16, 2006

Automating Windows with Autohotkey

Author: Dmitri Popov

If you want to automate some repetitive tasks or write a simple utility that makes your daily computing easier, it's not very practical to learn a full-blown programming language. Instead, you'd be better off using a scripting tool like Autohotkey that allows you to automate virtually any task on Windows.

Autohotkey features an easy-to-learn scripting language that allows you to create useful scripts in a matter of minutes. The scripting language contains functions that allow you to create graphical interfaces, effectively turning your scripts into mini applications. Better yet, with the Smart GUI tool, you can build graphical interfaces without writing any code at all.

To write Autohotkey scripts, you don't need expensive and sophisticated development tools or environments; a simple text editor will do just fine. And any script can be compiled into an executable file that can run on any Windows machine. (Autohotkey supports Windows 95 and upwards.)

Although you can use any text editor to write scripts, you might want to choose one that can work with Autohotkey's syntax, such as UltraEdit, Textpad, Emacs, or Vim. Autohotkey's Extras folder contains scripts and plugins for many popular text editors, including the excellent Notepad++.

The best way to learn Autohotkey is to create a simple script that does something useful. For instance, WordNet is an excellent online language reference tool. However, to look up a word in WordNet, you must select the word in your currently opened document, launch WordNet, or switch to it if it's already running, paste the word, and press Enter. This is not a big deal if you do it every now and then, but if you use WordNet often, you might want to reduce all the clicking and pasting to a single keyboard shortcut. Using Autohotkey, you can easily create a script that will do just that. Here's how.

Start by downloading and installing the latest version of Autohotkey. Create a new script by right-clicking on the desktop and choosing New -> Autohotkey Script from the context menu, then open the script in your text editor.

The first thing we want the script to do is copy the currently selected word into the Clipboard when the user presses the Ctrl-Alt-W keys. This can be done using the following command:

^!w::
Send, ^c

As you might have guessed, the Send command sends the Ctrl-C (copy) key combination. Note that the command itself is placed on a new line; this tells Autohotkey that the defined shortcut starts not a single command, but a whole sequence of commands.

Next, the script must launch WordNet using the Run command:

Run, wnb.exe

There is, however, a slight problem with this command: wnb.exe can only be executed in its own directory. Therefore, you have to define it first as a working directory:

SetWorkingDir, C:\Programmer\WordNet\2.1\bin\

Since Autohotkey supports environmental variables, you can make this command more flexible, so the script can run on machines where the Program Files directory is called something else (for example, it's called Programmer in the Danish version of Windows):

SetWorkingDir, %ProgramFiles%\WordNet\2.1\bin\
Run, wnb.exe

Now, the script must wait for the WordNet window to be opened:

WinWait, WordNet

and then make it active:

WinActivate

Finally, the script must paste the word from the Clipboard into WordNet and send the Enter command:

Send, ^v{Enter}

The sequence ends with the return command, which tells Autohotkey that there are no more commands left. The final result looks like this:

^!w::
Send, ^c
SetWorkingDir, %ProgramFiles%\WordNet\2.1\bin\
Run, wnb.exe
WinWait, WordNet
WinActivate
Send, ^v{Enter}
return

Your first script is ready to go, and you can launch it by double-clicking on it. When you see the Autohotkey icon in the System Tray, you can test your script by selecting a word and pressing Ctrl-Alt-W.

More fun with scripts

Autohotkey contains many other commands and functions that you can use to improve the script. Currently, the biggest problem with the script is that it opens a new WordNet each time you run it, so you can end up with a myriad of WordNet windows on the screen. One way to solve this problem is to instruct Autohotkey to close any WordNet window before opening a new one:

IfWinExist, WordNet
{
WinClose
}

You can also add some interactivity to your script, so that it asks you whether you want to look up the word in WordNet or Wikipedia:

^!w::
Send, ^c
MsgBox, 4,, Look up the word in Wikipedia?
IfMsgBox, Yes
{
Run, http://en.wikipedia.org/w/wiki.phtml?search=%clipboard%
Exit
}
IfWinExist, WordNet
{
WinClose
}
{
SetWorkingDir, %ProgramFiles%\WordNet\2.1\bin\
Run, wnb.exe
WinWait, WordNet
WinActivate
Send, ^v{Enter}
}
return

The fun, of course, doesn't have to stop here. If you're learning a new language and have a list of words to memorize, you can turn WordNet into a "Word of the Day" tool by using the following Autohotkey script:

^!w::
Loop, Read, C:\MyWords.txt
WCount := A_Index
Random, RLine, 1, %WCount%
FileReadLine, WordOfTheDay, C:\MyWords.txt, %RLine%
Clipboard = %WordOfTheDay%
SetWorkingDir, %ProgramFiles%\WordNet\2.1\bin\
Run, wnb.exe
{
WinWait, WordNet
WinActivate
}
Send, ^v{Enter}
return

The script reads the MyWords.txt file containing a list of words, picks a random word, and sends it to WordNet.

If you want to share your script with users who don't have Autohotkey installed on their machines, you can convert the script into an executable file by right-clicking on it and choosing Compile Script from the context menu.

Conclusion

Of course, Autohotkey's capabilities reach far beyond these simple examples. More importantly, Autohotkey features a combination that makes it a tool of choice for automating Windows: It's both powerful and easy to use. Whether you're a seasoned Windows administrator or an ordinary user, Autohotkey can make your computing life easier.

Dmitri Popov is a freelance writer whose articles have appeared in Russian, British, and Danish computer magazines.

Click Here!