Ok. Ok. What's next?
Ok. Ok. What's next?
I like to think of myself as a lazy programmer with a distaste for lazy code. Perhaps a brash statement to make, but I don’t feel any love for menial tasks that get in the way of good quality designing, coding and testing solutions. My absolute favourite way to combat some of these is by applying a nice dose of AutoHotkey (AHK) to help me out along the way.
Aside from being my favourite language, AHK is a very simple to use language that complements my lazy programmer attitude. AHK scripts are saved as raw text, so we can simply make a new .ahk file then edit it if you want to code along. To run a script just double click the file like a normal program and you’re done.
Defying convention, here’s not one but two hello world programs depicting two basic language concepts that are almost always used:
a::SendInput, Hello World!
After running the first example, whenever the “a” key is pressed, AHK will simulate “Hello World!” keypresses being sent to the current active window. In simple terms, it will type out “Hello World!” when you press “a”.
The second is similar, however uses what is called a hotstring. In this example, when the word “hi” is typed (followed by a space, newline or similar) the text “Hello World!” will replace it.
Things get more useful when we introduce sending special keystrokes. If you find yourself logging into test websites often, with throw away credentials, consider writing a quick script to automate that:
In this example, typing “login” into a username text box will send “Username” then tab to the next item on the page (usually a password text box) and send Password, then simulate an Enter keystroke and voila, you’re into the system.
We can extend the login example to automate more of the process. In one development system I find myself logging into frequently, there’s a button which is always in the same place, which I found myself always clicking, all the while wishing there was an easier way. Thankfully, there was:
Click, 100, 200
Now whenever we type in “login” not only do the username and password get sent, but the script waits for the page to load (using sleep, crude but effective – we don’t want to waste time trying to save time, right?) then clicks at the coordinates (100, 200) on the screen, all in one satisfying swoop.
If I were reading this blog, I’d be tearing my hair out – who on earth has time to look for the exact coordinates of a button? Not me, which is where this example may come in handy:
MouseGetPos, MouseX, MouseY
Clipboard = %MouseX%, %MouseY%
This script will trigger when alt (designated by the exclamation mark) and m is pressed. The MouseGetPos function puts the X and Y coordinate of the current mouse position in the output variables MouseX and MouseY (note we didn’t have to declare them before usage). Clipboard is a built in variable representing the “current contents of the Windows clipboard”, in which we place the comma separated mouse coordinates. Pay close attention that we have to dereference MouseX and MouseY by enclosing them in percent signs. Now we can simply paste into our script file where we wanted those pesky coordinates in the previous example, keeping this helper script in our back pocket for later, of course.
A further use case of AHK is opening up commonly run programs, a favourite of mine directly opening webpages:
Run, “C:\Program Files\Internet Explorer\iexplore.exe” %url%
Run, “C:\Program Files (x86)\Google\Chrome\Application” %url%
Run, “C:\Program Files (x86)\Mozilla Firefox” %url%
The run function is very versatile and reading the documentation page a goodway to see its true power, however here we use it to save time opening the same website in different browsers. This example also illustrates how to define your own function in AHK and invoke it in a hotstring or hotkey code block.
A final case can be used when you’re developing or testing software that has minimum resolution support:
WinGetPos, X, Y , , ,A
WinMove, A, , %X%, %Y%, %Width%, %Height%
This example uses WinGetPos to find the X and Y coordinates of the current active window (denoted by the final parameter which has been set to A) then passes the new width and height into the WinMove function, along with the old window position. This will set the current active window size to exactly the size passed into the ResizeWindow function.
So go forth and heat up your office with your blazing AHK skills, not forgetting to clue in your colleagues; we’re all on the same team after all. I think there’s a case to be made for shared scripts being created for large projects; a good proportion of a developer’s time is spent using other people PC’s (who always have everything setup in funny ways), and a shared AHK script could iron out some of the kinks, such as navigating to local repositories or quickly opening up another dev’s IDE of choice.
So to round up, AutoHotkey is great for automating otherwise menial tasks, and has so much more functionality than I have mentioned in this blog post that it always has a new surprise waiting for you. For me at least it is also fun to code in as you don’t have to get bogged down with things like declaring variables or strict syntax rules. It’s a fast, fun and efficient language which sits nicely in any developer’s tool box.