AutoHotKey Journey: Discovering Task Automation Techniques

Mastering Workflow Automation with AutoHotKey

Acquiring coding skills can be a challenging yet rewarding experience. In the beginning, I encountered difficulties with JavaScript and strived to mentally organize the components of my programs. My learning approach consists of 80% hands-on practice and 20% reviewing documentation repeatedly. Recently, my workplace faced a challenge in automating a workflow that previously relied on basic keyboard macros for copying, pasting, and formatting text.

The solution implemented months prior was no longer effective and functioned as intended only half of the time. This resulted in frustration and increased time spent on simple tasks. Recognizing the need for improvement, I took the initiative to develop a more efficient solution.

The Mission:

  1. Must work across Windows

  2. Must optionally be program-specific

  3. Must be easy to use and get started with

Part One: Exploring the Windows Mouse and Keyboard Center! This free application from the Microsoft Store enables macro recording and playback. At first glance, it seemed to have a user-friendly interface, and I was excited to discover a potential solution to our macro challenges. However, I may have missed some warning signs.

Upon using the macro recorder, I realized that without specialized peripheral hardware, the only button I could assign to a macro was the center mouse button. Initially, this didn't seem problematic since I only intended to create one macro. However, as I experimented further, I discovered numerous possibilities for automating daily tasks. Although limited to one macro at a time, I was able to work around this constraint while exploring the tool. I enjoyed the process of recording and executing macros, which significantly reduced my workload.

After creating several macros for tasks like exporting files, generating reports, and copy-pasting with formatting, I attempted to combine them into a single, comprehensive macro to address a major pain point in our workflow. Regrettably, I discovered that the Microsoft Mouse and Keyboard Center imposes a macro size limit, which hindered my progress nearly two-thirds of the way through.

After spending over an hour recording and testing keys for the macro, I was disappointed to find that my efforts were in vain due to the size limit imposed by the Microsoft Mouse and Keyboard Center.

Despite searching for ways to bypass this limitation, I was unable to find a solution. That night, I went to bed pondering potential alternatives. Upon waking up, I remembered using a scripting language to modify game saves in Borderlandsas a kid. I knew that others had used similar languages for automating tasks in games like Runescape, so I began researching various options along with their advantages and disadvantages.

This led me to discover AutoHotKey (AHK), an open-source scripting language introduced in 2003. Initially created for generating hotkeys, AHK is now widely used for macros and software automation. Surprisingly, I had never encountered it before, but found it to be both powerful and user-friendly. Additionally, there is a macro recorder called Pullovers Macro Creator (PMC), an open-source program built on AHK. I began using PMC, a straightforward macro recorder that allows exporting to AHK, and most importantly, it does not impose a size limit on macros.

I began by writing a simple script to move the mouse around the screen. PMC tracked the mouse's exact position on the screen, which was a breath of fresh air compared to the cardinal directions and floating DPI of the mouse and keyboard centers. This allowed me to use different starting positions and enable the macro from anywhere. It also let me bind and create various hotkeys so that multiple macros could be activated simultaneously! This was incredibly helpful as I initially learned the language, but in my opinion, it is much faster to use the code directly. It was a lot of fun learning the different commands and virtual key codes, which are the program's way of identifying which key is pressed. I quickly decided that, ironically, I wanted to maintain my original "one button for everything" design that I was forced into at the beginning with the mouse and keyboard center. This ensured that my code had high readability and usability, even if I could no longer maintain it.

To accommodate multiple macros, I programmed a basic GUI with a dropdown menu to select a macro and a start button to initiate the chosen macro. AutoHotKey makes this process easy.

Gui, Add, DropDownList, vMacroSelect, Copy-Paste-Format|Export All|Create Report|
Gui, Add, Button, gStartMacro, Start Macro
Gui, Show
return

I set this up with the code for my macros. The full code is on my Github, but here's an example:

Loop
{
    If SelectedMacro = "Copy-Paste-Format"
    {
        Send {Up 5}
        Send {Left 10}
        Send {Up 5}
        Send {Left 10}
        Send {Up 5}
        Send {Left 10}
        Send {Up 5}
        Send {Left 10}
        Send {Right 2}
        Sleep 10
        Send {Right 2}
        Sleep 10
        Send {Down 2}
        Sleep 10
        Send {Right 2}
        ; ... and so on
    }

In AHK, the ";" symbol represents a comment. This caught me off guard when I first started, but I was pleasantly surprised by how quickly I grew to like it.

I had my macro menu set up, and everything was ready to go! I ran the program, selected my macro, and pressed start.

I forgot to include the code to break the loop!

The macro went haywire, and I desperately tried to control the damage while wrestling my mouse into a secluded corner of my desktop to prevent it from clicking and accidentally copying or even deleting an important file. Eventually, I managed to stop the loop by turning off the computer.

After rebooting the computer, I added a kill switch. Now, if a user presses Ctrl + Alt + X, the program will immediately terminate itself and all subprocesses.

            If GetKeyState("Ctrl") && GetKeyState("Alt") && GetKeyState("X")
                Break

            Sleep 1000

Time to try again. I ran the program, opened the macro menu, selected my macro, and clicked Start.

The macro loaded, started, and worked perfectly! Just as I intended it to! I had successfully developed a massive time-saver for our team. Along the way, I learned a new programming language and the importance of always implementing a panic button.

In conclusion, AutoHotKey enabled the creation of an efficient and customizable macro solution to overcome workflow challenges in the workplace. Not only did it save time for the team, but it also provided an opportunity to learn a new programming language and the importance of implementing safety measures like a kill switch. Embracing AHK can lead to significant improvements in productivity and automation across various tasks.