AutoHotKey: a short introduction
- Dec. 26, 2018
Update April 2019: If you want to see a working AutoHotKey script of mine in the context of TM1: look here please.
You might yourself the question why it took me so long to post new articles ? On the one hand, there is less free time. On the other hand, I spent almost all available time with the automation language called AutoHotKey gedoken.
The regular visitor of this blog will not be swept away if I write that I highly value automation. That's what I do since long in both Excel (Office) and TM1. You can read about these programs in other areas of the site. But when it comes to real automation, we can go much further with AutoHotKey, even though in itself the scripting language is not that difficult. Obviously, it will be an advantage if you can already write programming code, if you want to master AutoHotKey in a relatively short period of time.
Within AutoHotKey we will define hotkeys, to which we tie actions. For example, on my laptop, Ctrl + Shift + F4 will jump to the most recent file in the Downloads folder. To accomplish this, we need to program and provide the instructions to AutoHotKey to respond to that hotkey. Among other things, we need to write a loop through the files in the Download folder and retrieve the file that has the most recent last modified date/time. Any time, in any program, you can execute the shortcut key. A new Windows Explorer window will open in the Downloads folder and have the most recent file selected.
Other hotkeys could be dependent on the context. For example, when I am working with appointment items in Outlook, I can press F10. A list will open up with the different choices for the subject of the appointment item: this greatly harmonizes and speeds up the input. To be sure, the list may only appear if I press F10 if Outlook is active, and only if I am working with an appointment item.
Another popular functionality in AutoHotKey is a hotstring. Like this you can type whenever and wherever, "btw", and will be instantly replaced with "by the way". A kind of autocorrection but much more useful and versatile.
Gaming freaks who know how to program, will love AutoHotKey !
The Windows key is a good candidate to build hotkeys. With me, Windows + c is linked to the windows Calculator. But that shows nicely the power and versatility of AutoHotKey: pressing this hotkey gives us several possibilities. If the calculator has not yet been started, it will be started. If the calculator is started but in a minimized state, it will be restored and shown on screen. If the calculator is shown on screen, the shortcut key combination will minimize it.
Be prepared to have to search in the beginning, at least it was like that for me, and it will be with nearly any language without a GUI (graphical user interface). That's why I enable myself to press Windows + g which facilitates:
- select 1 or more words
- press the hotkey
- an inputbox will show up with the selected words, you can overwrite them or choose words for yourself without selecting them in the first place
- press Enter
- result: your favorite browser will open up and will automatically do a Google search for 'autohotkey' followed by the chosen words
Who of you is using the square key (²) and the key for power 3 (³ or Shift + ²) - if you have it on your keyboard ? Or the Capslock key ? What if we were to make these keys really useful ? With me the square key will select and copy the entire line:
- simulate pressing Home: go to the beginning of the line
- simulate pressing Shift and keep it pressed: start with selecting text
- simulate pressing End: go to the end ofhe line
- simulate pressing Shift and release it: stop selecting text
- simulate pressing Ctrl + c: copy the text
In practice you can execute those steps rather quickly, but just pressing 1 key once seems easier and faster, more reliable !
Now, what if we happen to do this in Excel... ? Well, then I thought out the script differently. In Excel we will copy the entire cell contents:
- simulate pressing F2: go into the cell in edit mode
- simulate pressing Ctrl + a keystroke: select the cell contents
- simulate pressing Ctrl + c keystroke: copy the selected information
- simulate pressing Esc: stop edit mode
Then, what will be ³ ? With me it is pasting the clipboard contents over the entire line / cell contents. That's true, it's the inverse of ², which was used for copying information. Ctrl + ² can yield yet another script, etc. Combine Ctrl, Alt, Shift, Win, other keys, numbers, letters, characters, mouse gestures/buttons, etc. - possibly dependent on the context when they are executed.
There are many tools at our disposal in AutoHotKey. To name but a few, array's, string manipulations, working with files and folders, COM objects, built-in Windows functionalities, controlling the mouse or joystick, timers, sending certain keystrokes, executing commands repetitively, automate Excel, regular expressions, creating your own interfaces, ...
Your code, that you write in an *.ahk text file, can be executed on the computer. You can also compile the code to an *.exe file. That way you can create scripts relatively easily and make them available on different computers, without much effort and hassle. You can get the same level of efficiency on other computers, compared to your own computer. If, by doing that, you use small configuration file (*.ini files) then you don't have to adjust the code in the compiled *.exe files: you just adjust the settings in the configuration file and those settings are applied within the generic script.
There is an active user community for AutoHotKey. Join the community and ask questions, the entry barrier is not steep. AutoHotKey forums. Use the search functionality on the forum or on Google and you will often find read-made scripts.
Finally, I present one of my more advanced scripts for AutoHotKey. It saves me quite some time.
- We start off in Gmail, my favorite email program for private emails, using Google Chrome
- I download 1 or more files that a customer sends to me (for analysis, programming, testing, …)
- I press Ctrl + Shift + F5, when automatically:
- the notification bar for downloads at the bottom of the screen will close
- an inputbox will ask me the customer name (I can use numbers for the 5 most busy customers), or I can enter a new customer name
- a second inputbox will ask me how many of the most recent files will need to be moved to a custom folder for the customer
- based on the previous responses, a folder will be created for the chosen customer, with inside a folder for the current month (format: yyyy-mm), and again inside a folder for the current day (format: yyyy-mm-dd)
- the most recent downloaded files will be moved to the custom folder for today
- should zip files be downloaded and moved, they will be unzipped automatically, with original zipfiles removed
If you need to do this several times a day you will be happy to automate the thing ! It saves time and effort and caters for an unambiguous way of working. I wrote a number of other scripts too that save me more time, but they probably do not appeal that much to the casual visitor.
AutoHotKey, the next best thing in town… There's more to come in the next months ! Remember that a time gain of only 10 minutes per workday adds up a week of holidays on a yearly basis ! Now you only have to convince your boss :-)