Power Up Your Productivity on Windows with AutoHotkey Macros

Steven NgDaily Debug BlogLeave a Comment

AutoHotkey, which is on my big list of my favorite tools for Windows post, is something I rely on daily to boost my productivity.

While I primarily use it for text expansion, it is super powerful and can do just about anything.

In this post, I’m going to give you a little taste of what you can do with AutoHotkey.

Getting Started

  1. Go to AutoHotkey’s download page, and download the AutoHotkey Installer for the current version. At the time of writing, the latest version is v1.1.30.01 – November 11, 2018
  2. Run the installer and install the software.
  3. When that’s done, create a new text file called AutoHotkey.ahk in your Documents folder. You can actually call it whatever you want, but AutoHotkey.ahk is pretty self explanatory and a good place to start.
  4. Create a shortcut of your AutoHotkey.ahk file in Windows File Explorer. Copy the shortcut to your clipboard.
  5.  In Windows File Explorer, browse to your Start Menu folder. Your Start Menu folder is usually in this location (if you’re using a recent version of Windows):
    C:\Users\{your user name}\AppData\Roaming\Microsoft\Windows\Start Menu
  6. Substitite {your user name} above with your actual Windows user name so that you get the right path. Paste the shortcut in your clipboard to this location. This ensures that your AutoHotkey.ahk file starts your AutoHotkey script every time you log into your computer.

AutoHotkey Basic Concepts

The AutoHotkey.ahk file that you created in your Documents folder is your AutoHotkey script. It will contain all the code that AutoHotkey uses to listen for input events (whether it be from your keyboard, mouse or other input device) and fire off an activity.

Currently, your AutoHotkey.ahk script is empty, but I’m going to provide you with some samples to get started with. Detailed documentation on writing AutoHotkey scripts can be found on their website.

Unless you’ve rebooted or logged in and out since installing AutoHotkey, AutoHotkey is not running right now. To launch AutoHotkey, double click on your AutoHotkey.ahk file. Even though there’s no code, it should launch. You’ll notice a small green square in your taskbar with a white H in it like the screenshot below.

Every time you save a change to your AutoHotkey.ahk file, you will need to reload the script. To do that, right click on the taskbar icon and select Reload This Script. This will load any changes you have into memory. If there’s an issue with your script, you’ll get an error message. To fix this, you’ll need to go back to your text editor, fix the broken code, resave, and reload the script again.

If you ever need to edit your script, you can right click the AutoHotkey icon in the taskbar and select Edit This Script. It will open the text editor that is bound to the .ahk file extension, which by default is Notepad.

Now that you’ve got the basics, let’s dig a little deeper.

Text Expansion

Do you ever get tired of typing out your e-mail address, or a long path/command in your Windows Command Prompt? Well, this is where Text Expansion can save you a lot of headaches.

Let’s do your e-mail address first. Paste the bit of code below into your AutoHotkey.ahk file, save, and reload it into AutoHotkey.

::@email::my.name@mydomain.com

Now, open up a new instance of Notepad, and type in @email followed by the space, period (.) or return key. You’ll notice something magical happened. @email disappeared and was replaced by my.name@mydomain.com.

That was easy! So let’s break the code down so you can understand it. Take the first part of the code:

::@email::

In AutoHotkey, the text that triggers a text replacement is called a hotstring. A hotstring starts with a pair of colons. In this case, the hotstring is @email. The last pair of colons separates the hotstring from the replacement string that will be fired. Hotstrings are fired when you hit the space, period (.) or return key.

The second part of the code:

my.name@mydomain.com

That’s the replacement text that you want to replace the hotstring.

So following this pattern: ::hotstring::replacement text, let’s create another example.

Let’s say that you’re typing in some long path to get to your development folder in your Command Prompt, and you want to use @path to trigger it. Notice that I prefix my hotstrings with an @ character.

This isn’t required, but using a special character as a prefix prevents you from accidentally firing a trigger when you don’t want it to. Let’s say you decided to use the as your hotstring. Every time you typed the with a space, return or a period (.), it would fire your hotstring. Since @the followed by a space, return or (.) is uncommon, it’s less likely to fire your hotstring by accident.

Add this bit of code below into your AutoHotkey.ahk file, save, and reload it into AutoHotkey.

::@pf::C:\Program Files

If you open up your command prompt and type in:

cd @pf

And then hit return, the command

cd C:\Program Files

Will appear and execute. Not bad! What I’ve shown you is only the basics. You can do multiline text expansion and more. Check out the AutoHotkey docs for more advanced functionality once you’ve grasped the basics.

Application Launching

Another handy thing you can do is use AutoHotkey to launch applications.

Let’s do a quick example. Paste the bit of code below into your AutoHotkey.ahk file, save, and reload it into AutoHotkey.

#z::Run cmd

Now hold down the Windows key plus the Z key. The Windows Command Prompt should launch in a new window.

So let’s break the code down so you can understand it. Take the first part of the code:

#z::

Notice that unlike the hotstrings, the command does not start with a pair of colons. Instead, we see a hash (#). The hash is the Windows key. So #z means Windows key + z. Because this command executes something instead of replacing text, it’s called a hotkey in AutoHotkey speak. The two colons at the end separate the hotkey from the command to be executed.

The second part of the code:

Run cmd

Run is a Autohotkey command which launches whatever follows it. cmd is the Command Prompt, but you can replace it with Notepad to launch Notepad.

Now the reason why cmd and Notepad work without knowing the full path to the executables is because those apps are in the PATH environment variable. If you’re trying to run an application that is not in the PATH, then you need to put the full path to the exe after the Run command.

Another trick you can do with a hotkey is to launch a URL. Paste the bit of code below into your AutoHotkey.ahk file, save, and reload it into AutoHotkey.

^!+Z::Run https://assignittous.com/daily_debug_blog/

Now hold down ctrl + alt + shift + z altogether. You’ll notice that this very blog will launch in a new browser tab/window (depending on your settings).

Once again, let’s break down the code in pieces:

^!+Z::

The special characters ^!+ are Autohotkey’s notation for the modifier keys Control, Alt and Shift, respectively. The Z represents the z key on your keyboard. The two colons at the end separate the hotkey from the command to be executed.

The second part of the code:

Run https://assignittous.com/daily_debug_blog/

You’re already familiar with the Run command. The second part is simply the URL to this blog. When you fire the hotkey, AutoHotkey will launch that URL in the default browser specified in Windows.

Context Sensitive Hotkeys and Hotstrings

Everything I’ve shown you above are global hotstrings and hotkeys. That means they fire no matter where you are in Windows.

Sometimes you want to limit where hotstrings and hotkeys can run. You can do this by creating context-sensitive hotkeys and hotstrings. It’s a little more complicated, but it’s easy to get started.

Let’s take this example from the AutoHotkey documentation:

#IfWinActive ahk_class Notepad
^a::MsgBox You pressed Ctrl-A while Notepad is active. Pressing Ctrl-A in any other window will pass the Ctrl-A keystroke to that window.
#IfWinActive

Add the code above to your AutoHotkey.ahk file, save and reload.

Open up an instance of Notepad, and hold down control+a

A popup with the message “You pressed Ctrl-A while Notepad is active. Pressing Ctrl-A in any other window will pass the Ctrl-A keystroke to that window.” should appear.

Let’s break down the code line-by-line:

#IfWinActive ahk_class Notepad

#IfWinActive is an AutoHotkey directive to set the context of an active window (i.e., an application in the foreground).  That means all hotkeys/hotstrings below that directive will apply to the context set by this #IfWinActive directive until it is closed with an empty #IfWinActive directive (this is an oversimplification as I’m trying to keep the examples appropriate for beginner level users).

ahk_class is a Window Class. To oversimplify, a Window Class is Microsoft Window’s secret name of the window. Sometimes the names are obvious (as in the case of Notepad), sometimes they’re not. Fortunately, AutoHotkey gives you a tool to figure out the name of a Window Class, and I’ll show you how to use it after I finish explaining this code.

Using plain English, the directive #IfWinActive ahk_class Notepad translates to “If the active window (in the foreground) is a Notepad window, then the following hotstrings and hotkeys will only apply to that window until I close the directive”

Now for the second line:

^a::MsgBox You pressed Ctrl-A while Notepad is active. Pressing Ctrl-A in any other window will pass the Ctrl-A keystroke to that window.

^a:: is the hotkey notation for control+a  The two colons at the end, separate the hotkey from the command to be executed.

MsgBox You pressed Ctrl-A while Notepad is active. Pressing Ctrl-A in any other window will pass the Ctrl-A keystroke to that window. — This is the command to be executed. MsgBox is an AutoHotkey command that will open up a popup with a message. All of the text after the MsgBox command is the text that will be put into the popup message.

Finally, the empty #IfWinActive closes the first directive. All of the commands below the empty directive will be global unless you use another #IfWinActive directive to specify a new context.

How To Determine A Window’s Class Name

Thanks to AutoHotkey’s Window Spy tool, it’s easy to figure out a Window’s class name.

From the taskbar, right click the AutoHotkey icon, and select Window Spy from the context menu.

A Window Spy popup will appear:

If you start clicking on application windows, the contents of the “Window Title, Class and Process” box at the top of the pop-up will change.

The contents of the box are split into 3 lines.

The first line is the Window Title, which matches the title on the top of the window bar (if your window has one). This helps you identify if you clicked the correct window.

The second line, which is the one you care about, contains the Window Class. In the example above, I clicked on a Notepad window, and the class is ahk_class Notepad. If you click on a Chrome window, that second line will probably say ahk_class Chrome_WidgetWin_1 (depending on what version of Chrome you’re using).

To create a context sensitive directive for Chrome for example, you would use this:

; lines that start with a semi-colon are comment lines and are
; ignored by the AutoHotKey script parser
; the line below sets context for Chrome
#IfWinActive ahk_class Chrome_WidgetWin_1 
; hotstrings and hotkeys go here:
^a::MsgBox You pressed Ctrl-A while Chrome is Active!
; the line below ends the context for Chrome
#IfWinActive

Now Try It Yourself

Now that I’ve given you a taste of what you can do with AutoHotkey, give it a shot!

Keep in mind, I’ve only scratched the surface of what AutoHotkey can do. It’s incredibly powerful, and I strongly suggest you take a look at the AutoHotkey docs to learn more about its features.

 

 

 

Other articles by this author:

Please follow and like us:
error

Leave a Reply

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