RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


AutoHotKey: Automate Windows with a Third-party Scripting Language  : Page 2

If you are the type of person who finds keyboard inputs such as HotKeys convenient for controlling Windows, read on. AutoHotKey is a simple scripting language that will allow you to extend HotKey functionality and send inputs to applications.

Using AutoHotKey to Make HotKeys
The following is an example of a functioning AutoHotKey script from the AutoHotKey documentation:

    #n::Run Notepad
The # sign is a shortcut reference to the Windows Key found on many keyboards, the n is the letter n, the double : assigns the Run Notepad to the keyboard combination of the Windows Key and letter n pressed simultaneously. Of course, the Windows Key is already used for a lot of hotkey combinations, pressing it alone opens the Start menu in Windows XP, while pressing the Windows Key and the letter e simultaneously opens Windows Explorer rooted on the 'My Computer' special folder. Remember that overriding Windows key functionality can have unintended side effects if you are not careful.

Run is a command that runs an external program. If you pass it a system-wide command (for those applications contained in the Windows folder), such as Notepad, it will execute that command. If you pass it a predefined registry clsid, such as the clsid for My Computer, - 20d04fe0-3aea-1069-a2d8-08002b30309d, it will open the My Computer folder, as shown:

 #e:: Run::{20d04fe0-3aea-1069-a2d8-08002b30309d}
This combination overrides the Windows Key and letter e combination to do exactly the same thing that Windows does anyway. If the string is the name of a file or shortcut in the same folder as your AutoHotKey script it will open that file or shortcut; if the string is a URL it will open that URL in your default browser. If a file, command, or URL cannot be found it will return an error message. (See the sidebar "Three HotKey Examples, One Error Message," for more examples.)

One of the benefits of Run is that it will also pass parameters to the application it launches, for example the command below not only runs Internet Explorer but it tells IE what Web address to direct to:

    Run Internet Explorer http://www.devx.com 
What if you want to open an application and send keystrokes or other input to it? The way I have been doing it so far won't work because text to the right of the file or URL in the Run command is a parameter passed to the appropriate application. Here's one solution that performs several actions from one key combination:

Run Notepad
Sleep, 200
Send "hello Notepad"
This example allows multiple actions and introduces two more commands. The Sleep command pauses script execution for a time between 0 milliseconds and 24 days. For the uses demonstrated in this article most sleep times will be a few hundred milliseconds—long enough to open the new application. The Send command sends the sequence of keys found in between the quotation marks. Note that this is somewhat fragile: If you want to make sure Notepad is open before sending the text to it there are ways to wait until the opened application has the context. For a quick and dirty automation, though, this is more than adequate.

Author's Note: An alternate syntax would be to use the & sign to separate the keys in your HotKey combination, for example # & n:: Run Notepad works the same as #n: Run NotePad. The reason for doing this is that you may want to separate named keys, such as the space key from single character keys such as n.

Remapping Keys with AutoHotKey
You could use what we've already learned to remap keys with AutoHotKey, for example:

remaps the letter x to the letter y. Reasons to remap keys could be:
  1. You have a 'non-standard' keyboard .
  2. You want to send characters that are not otherwise accessible.
  3. You find certain keys easier to use than others.
  4. You want to use a non-standard keyboard layout, such as the Colemak keyboard layout implemented using AutoHotKey
One thing that should be noted is that remapping is context insensitive when using lower-case, so that remapping x::y will also remap X::Y, but if you use upper case only the upper case is remapped. For example X::Y remaps uppercase X to uppercase letter Y but the lowercase x will be unaffected while the following remapping x::Y, remaps both x and X to Y. (See the sidebar "Simple, Impermanent Mapping," for more on the pros and cons of remapping keys.)

Remember that other input devices can be remapped besides keyboards. The following remaps the right mouse button to the letter y (no reason to do that, it's just an example):


A Simple Example of Usage
It is useful to provide HotKeys running not just in the operating system but also in specific applications. One strategy is to have a script with a set of HotKeys useful for working a particular application. When you intend to work with that application you Run the script from your script with starting hotkeys. This could also be achieved by having the script check if the application you want to run your hotkeys with is active, but I prefer to do it with a hotkey that I can control.

Below is an example AutoHotKey script for starting more scripts. It uses the Windows Key + g keystrokes to Run the address of gmail and then load in a script called gmail.ahk. The same thing is done with the Windows Key and the r key for Google Reader.

  Run http://mail.google.com
  Run gmail.ahk
  Run http://www.google.com/reader
  Run reader.ahk ;this overrides the Windows hotkey for search you may want to change it
#p:: run Acrobat.ahk
The different scripts started of course need to be defined. Gmail.ahk, which provides some hotkeys for use with Gmail, is shown in Listing 1; Reader.ahk, which shows some hotkeys for use with Google Reader, is shown in Listing 2; and Acrobat.ahk, which has some hotkeys for Adobe Acrobat, is shown in Listing 3.

As you can see from the three listings you now have functions. The functions, although with different names in each example, are all essentially the same. They take a numerical parameter and send a sequence of keys, after which they recursively call themselves, passing the parameter subtracted by 1 until the input parameter equals 0. If the input parameter equals 0 the functions won't send any keys. (See the sidebar, "Automating Text Output," for an example of this method.)

Listing 1 (gmail.ahk) is the only example that should require more detailed explanation. Basically it has different ways to select and deselect multiple emails at a time. The keys j and x are used by Gmail for their built-in hotkeys. The j key moves down the list of selected emails, and the x key selects an email. Note that you will have to make hotkeys in Gmail active. Because of built-in hotkeys you can essentially make super hotkeys that do a number of common hotkey combinations at once. (Two things to note quickly here: The number 2—as used in Listing 1—is the number at the top of a standard keyboard; the number 2 on a numerical pad is accessed by a different character. Hitting the delete button and the down arrow button simultaneously when the gmail.ahk is running will create an inputbox. The numerical value passed to this inputbox will cause the function to select a number of emails down from the current context. The delete and up button will select emails up from the current context.)

Once you've loaded in your application-specific AutoHotKey script you will want a way to unload it too. This is provided by the command ExitApp, which is used to exit a script with hotkeys in it. The keys used are the delete and the uppercase G for the gmail.ahk. In the reader the delete and uppercase F is used; and in the pdf the delete and uppercase P is used. You will remember that these are the same lowercase keys that were used to open the scripts. With a lot of extended hotkey functionality in your system you will want an easy way to start, shut down, and list the hotkeys in your scripts. Listing of hotkeys is provided by the command: ListHotkeys.

Each of the scripts has a hotkey that calls ListHotKeys with the insert key and the same letter that is used to start the script from the master script.

Taking AutoHotKey Further
I hope this article gives a nice overview of some of AutoHotKey's capabilities. Some other things that AutoHotKey makes especially easy that this article does not cover are:

  1. HotStrings (Make sure to check out this cool AutoHotKey script for Gmail from LifeHacker, based on the HotStrings concept.)
  2. Remapping joystick controls
  3. Control individual windows (including partial transparency)
There are lots of little open source scripts written in AutoHotKey that give you some productivity enhancing functionality. For example sites such as 1- Hour Software have a great number of useful widgets. The AutoHotKey forum also has useful scripts to download and customize.

Bryan Rasmussen is employed by the Danish Government's Data standardisation Project OIOXML with an overview of core XML and web technologies. For the past couple of years he has worked on the Danish implementation of UBL. His writings for DevX are in no way related to his work for the Danish Government.
Email AuthorEmail Author
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date