Keyboard macros with AutoHotkey

By Jon: First published in Online Currents – 20 (7) September 2005


Many years ago, at university, I overheard a snippet of conversation between two female students: “I’ve heard of garlic bread,” said one, “but I’ve never tried it.”

Many people feel the same way about macros.  They know that it’s possible to automate repetitive processes on the computer by creating macros, but it all seems too complicated.  There are too many decisions.  Local or global?  Relative or absolute?  Keystroke or toolbar button?  Also, how to create a macro that involves two or more programs?  

AutoHotkey, by Chris Mallett, is a freeware Windows macro system, which is simple enough to learn in a few minutes, but powerful enough to take over most of your repetitive tasks.  It sits between the keyboard and the computer and, when triggered by a keystroke, it will fire off a sequence of keys, mouse clicks and other instructions.  For everyday purposes it offers a simpler alternative to writing and debugging complex Microsoft Office macros in Visual Basic for Applications.

Useful AutoHotkey scripts can be compiled and distributed as executable (.EXE) files, which can then be run without the AutoHotkey program. 

How it Works
AutoHotkey can be downloaded from http://www.autoHotkey.com (the file size is about 1.5 Mb) and installed in a few seconds.  I recommend adding a shortcut to the program via the Startup folder, so that it runs whenever the computer is switched on.  When AutoHotkey is running, a small icon with a capital H appears in the tray at the bottom right of the screen.

In order to activate the program, the user must create and save a text file called ascript.  The script consists of one or more sequences of commands written in a simple scripting language.  For example, the following script reproduces the keystrokes involved in typing my name: 

Send, Jon Jermey

To associate this with a particular keystroke, I precede it with the name of the keystroke and two colons, and follow it with the word ‘Return’.  AutoHotkey recommends that you use the Windows key (represented in the script by a hash symbol), which is found on most modern keyboards and is otherwise barely used.  Here I have associated the script with the Windows-key, plus j:

#j::

Send, Jon Jermey

Return

To make this command available at any time, I double-click on the name of the file in Windows Explorer; it is then loaded into memory by AutoHotkey.  From then on, until the end of my computer session, any time I want to enter my name, I simply pressWindows-j.  It doesn’t matter which program I am in; I can be running Word or Excel or browsing the Web.  As AutoHotkey runs on top of Windows, it can communicate with any Windows program.  Many macros can be included in one text file, or the user can create different text files to be called up in different situations.  Macros can be halted at any time by pressing the Escape key, and a malfunctioning macro file can be called up for editing with a single click and quickly reloaded after modification.

In addition to the Windows-key combinations, macros can also be activated by other key combinations or even a sequence of keys; for example, loading the following single-line script will cause AutoHotkey to type ‘indexing services’ whenever I press ‘iserv’ followed by a space, comma or full stop:

::iserv::indexing services

Key Sequences
Key sequences in AutoHotkey can include Ctrl, Shift and Alt key combinations (represented by ^, + and ! respectively), in addition to all the non-text keys, such as Home, End and Delete, the names of which are enclosed in curly brackets. The following script will cut (^x) the currently selected item, move down two rows and paste it back in again (^v) when the Windows-d key combination is pressed:

#d:: 

Send, ^x{DOWN}{DOWN}^v 

Return

Longer sequences can be broken up into several Send commands, and interspersed with comments, preceded by semicolons:

#d:: 

   ;Cut and paste in two rows down 

   Send, ^x{DOWN}{DOWN}^v 

   ;Go down two more rows and delete 

   Send, {DOWN}{DOWN}{DEL} 

   ;Return to the starting point 

   Send, {UP}{UP}{UP}{UP} 

Return

 Mouse Actions
AutoHotkey can also be used to reproduce mouse clicks.  You can look these up with the accompanying utility program called Window Spy, and then add them to a script.  Here is a left mouse click at pixel location 332 across and 217 down, from the top left corner of the active window:

MouseClick, Left, 332, 217

Other mouse operations include moving, dragging and scrolling the mouse wheel.

Multiple Programs
AutoHotkey really shines when a macro has to operate across two or more applications programs.  Here is a macro which opens a spreadsheet in Excel, selects the contents of cell D4, copies it to the Clipboard and adds it to the end of a Word file, closing Excel and Word as it goes:

run, Budget.xls, C:ExcelDocs ;open the Excel file 
WinWait, Budget.xls  ;wait for it to appear 
Send, ^{HOME}{DOWN}{DOWN}{DOWN} ;move to A1 and down 3
Send, {RIGHT}{RIGHT}{RIGHT} ;move across 3 
Send, ^c   ;copy
WinClose, Microsoft Excel ;close Excel 
Send, N ;don’t save changes
run itemlist.doc, C:WordDocs ;open the Word file
WinWait, itemlist.doc ;wait for it to appear
Send, ^{END}{ENTER}^v^s ;go to the end, add a line,
;paste and save 
WinClose, Microsoft Word ;close Word

Programming Options
AutoHotkey is a fully-functioning programming language, and advanced users can create elaborate sets of instructions, including loops, if…then statements, and variables.  User input options allow a macro to be paused while the user enters a file name, or specifies the number of times to run through a particular loop.  It also connects with the Windows API, giving it access to the Clipboard, the Registry, and the internal clock, so that actions can be set to occur at a given time or after a specified period.  It can open, close, hide or show program windows, open and close CD drives, and bring up forms in dialog boxes to collect complex information from the user.

Conclusion
I’ve tried many macro programs over the years, and AutoHotkey is the only one I feel inclined to stick with.  I’ve got my own personal macro collection now, which runs at Startup and loads a variety of scripts to help with indexing, printing, scanning, and audio recording – all repetitive jobs, which have taken far too much of my time in the past.  I add new ones as required and eliminate ones that have served their purpose.  The Help system is excellent, a tutorial is available at the http://www.authoHotkey.comWeb site, and there is a support group at http://groups.yahoo.com/group/AutoHotkey .  Try it!  AutoHotkey may just be the utility you’ve been looking for.