AutoHotkey: A perfect tool to Automate Tasks on Windows

Detailed AutoHotkey guide to help you automate tasks on Windows

by: - Last updated on: April 17th, 2021

Key Takeaways

  • Interested in automating the mundane tasks on your Windows machine? Here’s how you can do it by using AutoHotkey.
  • AutoHotkey is a third-party, free, and open-source tool that allows you to automate operations on your machine.
  • AutoHotkey scripts can help you launch certain apps or websites, repurpose the function keys or the special keys, among other things.
  • You can also create scripts to do things like pull up a Google search for content in your clipboard, clean the Recycle Bin with just a click, shorten URLs, disable trackpad while typing, and more.
  • For more such in-depth guides, check our Tutorials hub.

Do you prefer automating the mundane and repetitive tasks on your computer to simplify your workflow? If you do, you have one of the best automation tools, Automator, on macOS, which allows you to create scripts to automate your tasks on the Mac. However, if you belong to the other end of the spectrum — who use a Windows machine — the closest thing to Automator on Windows is AutoHotkey or AHK.

AHK is a one-stop tool for all your automation needs. You can use it to create custom scripts and shortcuts, remap keys, or even create macros for different actions. So if you plan to automate tasks on Windows and are oblivious to AutoHotkey, here’s a guide to help you get started and use the tool to streamline your workflow.

AutoHotkey (AHK) Windows automation

When it comes to automation, be it on Linux, macOS, or even Windows, there are a plethora of automation tools, built-in or third-party, to help you create macros, custom scripts, and shortcuts. However, not all of them offer granular control over the different system actions, as well as third-party app actions. And this is precisely where AutoHotkey comes into the picture.

What is AutoHotkey?

AutoHotkey is a free and open-source tool, specifically for the Windows operating system, which allows you to automate a lot of the operations on your machine. It was released in 2003. And back then, it started as a macro and keyboard-shortcut creation tool that offered the ability to automate repetitive tasks. But over the years, it underwent various changes to finally emerge as the one-stop automation tool for Windows that it is right now.

AutoHotkey Windows scripting

AutoHotkey script to import Python libraries and modules

Unlike some of the other automation and custom shortcut creation tools, which offer a graphical interface (GUI) to make the tool beginner-friendly and convenient to use, AutoHotkey is strictly a command-line (CLI) tool. What this means is that you do not get a graphical interface with buttons and components laid out upfront to interact with. Instead, what you have is an entirely command-based interface that accepts inputs via commands and performs functions accordingly.

While there are a lot of advantages to this approach, there are also a few drawbacks since, unlike a GUI tool, its CLI equivalent is not the most user-friendly, and therefore, it requires the user to have a basic understanding of the know-how to be able to use it to its fullest potential.

How to Install AutoHotkey?

Now that you have an idea of what AutoHotKey is, the first thing you need to do to get started with the tool is to download and install it on your machine. Here’s how.

1. Head over to AutoHotkey’s official website and download AutoHotkey on your computer.

2. Next, run the installer and click your way through the installation dialog.

3. When presented with the installation type selection window, select Express Installation: if you want to install the tool with the default configurations, or select Custom Installation: if you need more control over the tool and want to configure the installation to your preference.

How to Use AutoHotkey?

Once AutoHotKey is installed on your Windows computer, the setup window presents a few options. You can either select to run AutoHotkey from here (for now) or click on View the Tutorial to get the complete documentation of AutoHotkey. If you plan to create custom scripts for automating tasks, this documentation can come in handy in helping you learn about the scripting syntax and all the fundamental elements.

In order to use AutoHotkey, make sure you have the tool running in the background. You can do this by going into the Start menu and running AutoHotkey from there. Once the tool is up and running, you now have two different approaches to automate tasks on your machine: you can either create your own custom scripts from scratch for actions you want to automate, or you can find and download scripts (created by other people) on your machine and run them.

A premade AutoHotkey script

Premade AutoHotkey script to minimize an app to the tray menu

If you are just getting started with AutoHotkey, it is understood that you do not have the required comprehensive knowledge of the scripting language to create a custom script. And therefore, using pre-made scripts (created by other people) is a safe place to start, not to mention, it can also help you understand and learn how scripting works on AutoHotkey so that you can eventually create a script yourself.

When it comes to finding ready-to-run scripts, some sources (source 1, source 2) on the internet offer a collection of various scripts for different tasks, which you can download and run directly on your Windows computer. Moreover, you can also find some pre-made scripts on AutoHotkey’s official website and forum. Further, in case you run into issues, you can also get answers to your queries on the forum. [Note: when you download a script, make sure the file ends in the .ahk extension.]

Once you have downloaded the file, you can then perform a few different operations on the script. Just right-click on the file and choose an option from the list. Basically, there are three useful functions: Run Script, Compile Script, and Edit Script.

Run-Compile-Edit AutoHotkey script

Run Script, as it sounds, executes a script. If you feel like mapping this function to a key, you can do that to simplify the workflow. On the other hand, Compile Script helps you compile the script and bundle it with an executable to convert the script into an executable (.exe) file. And the Edit Script option is for when you need to view the code or want to make changes to the script.

When you run a script, it moves to the system tray and runs in the background. At times when you need to pause the script, stop it, or suspend hotkeys, you can do so by right-clicking on the ‘H‘ icon in the system tray and selecting the suitable option from there. A thing to keep in mind when you make changes to a script is that every time you do edit the script, you need to reload it for the changes to take effect.

Reload-Pause-Suspend AutoHotkey script

On the other hand, if you plan to create a custom script yourself, right-click on the desktop and select New > AutoHotkey Script. Give a name to the file and make sure it ends with the .ahk extension. Now, right-click on it and select Edit Script from the list of options. Once the file opens in a text editor, start adding code. When finished, make sure to save the changes.

How AutoHotkey works, and how to create a custom script?

In simple terms, AutoHotkey does not do anything on its own. Instead, it relies on scripts — programs that contain instructions to perform an action — to automate tasks. A script is written using a text editor. You can even write it on the Notepad app that comes pre-installed on Windows. Or, if you prefer syntax highlighting, you can use any of the popular text editors like Emacs, Vim, Sublime Text, Notepad++, or SciTE4AutoHotkey, to code a script. However, if you are not familiar with text editors, you can either use Notepad++, which is a pretty basic editor (and relatively much easier to use) or, you can get the SciTE4AutoHotkey editor, which is a custom version of the SciTE editor, tailored to write AutoHotkey scripts — although it does have a learning curve.

AutoHotkey scripts can contain a single or a bunch of different actions. Ideally, you add a hotkey and specify an action you want to perform using it. However, for tasks that require multiple actions, you can also specify more than one action for a hotkey.

Writing a custom AutoHotkey script

AutoHotkey script to do a Google search on what’s in the clipboard

Talking about scripting, the tool understands the AutoHotkey scripting language, which, just like any other programming language, constitutes various elements such as variables, objects, functions, control flow statements, etc. When put together in a cohesive manner, these elements help you in performing your desired actions, and in turn, automating your tasks.

Without going into much detail, there are a few basic elements or functions you need to keep in mind to get started with custom scripting. These include:

1. Hotkeys – a single key or a combination of keys that you can bind to an action to execute it quickly.

Eg 1:
^c: :
Run, chrome.exe

Eg 2:
^h::
{
Send “Hello,” {enter}
Send “How have you been?”
}

Translation 1: an action that runs Chrome when you press the control + c key combination.

Translation 2: an action that triggers when you hit the control + h key and inputs Hello, How have you been?

The word enter is placed within parentheses because we do not want to add it as text. Instead, we want it to register the enter key and start the next sentence on a new line.

2. Hotstrings: a function that expands abbreviations, similar to auto-replace functionality.

Eg:
::omw::On my way{!}

Translation: expands the omw abbreviation with its full form. The exclamation mark is held inside parentheses to register it as an exclamation mark and not as an Alt key.

Essential symbols (or modifiers) you must know:

  • # is Windows
  • ! is Alt
  • ^ is Control
  • + is Shift
  • & is ampersand (used to combine two components)

3. Sending keystrokes: Send is an AutoHotkey function that allows you to capture the entered (typed) key and send it to a program. So, if you want to simulate a keypress, this function helps you achieve just that. There are various parameters that you can send to a program. And depending on what it is that you want to send, there are methods to help you with the same.

Eg:
Send “!fs”

Translation: Selects the File > Save menu.

Essential symbols and their functions:

  • ! is Alt
  • + is Shift
  • ^ is Control
  • # is Window

4. Running programs and opening websites: Run is one of the most useful functions, which lets you run programs or open URLs (websites). For scripts that require you to open an app or a website, this function is what you need. Besides, you can bind the function to a key to create a hotkey and perform an action.

Eg 1: Opening a website
^g::
Run, “https://www.google.com"

Eg 2: Running an app
^n::
Run, “notepad.exe”

Translation 1: opens Google search in your default browser.
Translation 2: runs the Notepad app.

5. Commands and Functions: commands and functions are two of the most crucial elements when scripting with AutoHotkey, which makes it easier to give instructions to a program. On the one hand, you have commands that take simple instructions and solve a problem, whereas, on the other hand, there are functions that help you supply complicated instructions to a program.

Syntax:

  • command, parameter 1, parameter 2, parameter 3
  • function(parameter 1, parameter 2, parameter 3)

Basically, it is the parentheses that separate command from a function.

Eg: Run, chrome.exe

6. Miscellaneous: alongside commands, functions, and the built-in functions like sending keystrokes, there are a plethora of different functionalities that AutoHotkey allows you to use in your script. For instance, you can use variables to store the output of a program, have an object to store the state of programs, use control flow statements to modify the input, output, and action of a program accordingly, use math functions, mouse functions, monitor functions, file system functions, among other things. Basically, everything that you can do with other programming languages can be done (to a certain degree) using AutoHotkey.

What are some use-cases of AutoHotkey?

One of the many possible use-cases for AutoHotkey is where you can create a script to add certain lines of code. That way, you can save some time and effort that you otherwise would spend writing repeated lines of code over and over again. If you work with, say, Python or any other programming language, you can create a hotstring that can fill in the defined lines of code.

For instance, if you use Python, it is likely that you require the need to import certain libraries and modules. In that case, the following code snippet can help you save some time and streamline your workflow.

Eg:
::impl1::
Send "import tkinter" {enter}
Send "import numpy as np" {enter}
Send "import random" {enter}

Translation: the above hotstring writes the lines in between the double quotes, one below the other, as soon as you type impl1 in your text editor and hit tab. The word enter, within parentheses, as already mentioned, is used to break a line. So what you essentially get is the following:
import tkinter
import numpy as np
import random

Similarly, if you need to write multiple style codes in CSS, you can have a hotstring word with the declared set of lines you need to input.

Custom AutoHotkey script to clean Recycle Bin

Custom AutoHotkey script to clean Recycle Bin

Besides its use in coding, you can also have AutoHotkey scripts to launch certain apps or websites that you often use on your machine. You can even bind keys to certain functions across different apps to perform them quickly and save a few extra steps. In addition, another useful application for AutoHotkey is to repurpose the function keys or the special keys (CapsLock, Insert, Esc, and the likes) to something that helps you trigger your desired action.

Eventually, as you get confident with scripting, you can create a script that performs a quick (Google) lookup on the internet for whatever you have in your clipboard. That way, you do not need to open the web browser and search for your query manually. Similarly, if you are tired of clearing the Recycle Bin manually to free up storage space, you can also create a script that clears the Bin for you instantly.

Needless to say, once you get the hang of the scripting language, you can practically achieve whatever functionality you think of performing with AutoHotkey in a more efficient manner.

Weekly Newsletter

Sign up for a specially curated Tech Newsletter.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *