Beginner’s Guide to How AutoHotkey Scripts Work

Exploring the Existential Mysteries of AutoHotkey Code and How It’s Often Misunderstood

AutoHotkeyInsightsThis topic may be a bit esoteric, but you’ll find it a tremendous help if you understand how AutoHotkey processes AHK scripts files. Quite a bit of the confusion encountered by novice AutoHotkey scriptwriters comes about because of misunderstandings about the way everything (life, the universe, and AutoHotkey scripts) fits together. I wrote the blogs with that potential confusion in mind.

*          *          *Cover 250 Border

New to AutoHotkey? See “Introduction to AutoHotkey: A Review and Guide for Beginners.”

*          *          *

I have since combined these efforts into a book titled, Beginning Tips for Writing AutoHotkey Scripts. It starts with an updated version of “How AutoHotkey Reads Scripts (AutoHotkey Script Structure).”

I wrote Beginning Tips for Writing AutoHotkey Scripts not as a tutorial, but as a guide to understanding how AutoHotkey processes scripts and how you can better design applications. For many new AutoHotkey users, the actions AutoHotkey takes when loading a script appear enigmatic causing unpredictable results. While initially many features may not seem to add up, AutoHotkey reads a script in a way that makes total sense—once you comprehend it. You’ll find understanding how AutoHotkey gets things done important for writing and debugging robust scripts.

Rather than a plethora of programming tips, I offer a framework for viewing AutoHotkey scripts and the scriptwriting process. If you understand everything in this book, then you’ll find writing and debugging AutoHotkey scripts much easier. I include “Best Practices” which make your AutoHotkey life much easier—especially if you plan to later integrate them with other scripts while maintaining their standalone capability. While I include numerous snippets of code in the book, they offer either basic level examples or insight into how AutoHotkey works.

I emphasize the modularity of AutoHotkey throughout the book. That means for the most part you can place encapsulate modules almost anywhere in a script—with a few exceptions. This modularity makes AutoHotkey a very forgiving language, but the exceptions to the rule can be killer.

In Chapter One, I explain how AutoHotkey loads a script in two phases. It reads everything in the first pass but doesn’t take any action until the second pass. I review the key components which might appear in a script and discuss when AutoHotkey interacts with each. You’ll find knowing what happens when in these two passes important to your script design.

Chapter Two takes a close look at how Hotstrings affect a script. Each type of Hotstring acts as a self-contained module that may appear almost anywhere in a script. I explain the three different Hotstring formats—each acting as an encapsulated piece of code.

Similar to the script interaction of Hotstrings, Chapter Three discusses Hotkeys and how to add them to a script. Usually encapsulated, you can place Hotkeys almost anywhere in the script. Examples span from one-liners to adding entire apps to a script with a single Hotkey combination.

Functions provide one of the most flexible methods for adding features to an AutoHotkey script. Especially useful for repetitive tasks, functions allow flexibility in script placement not available to other code snippets. However, to properly implement them, you must understand the difference between local variables and global variables. Chapter Four provides insight into using functions in AutoHotkey scripts.

One of the most common sections of an AutoHotkey script, the Label subroutine, executes the action. Similar to functions, AutoHotkey accesses these subroutines in a number of different ways. Chapter Five shows methods for directly executing subroutines (Goto and GoSub commands), as well as, Hotkeys set up with the Hotkey command.

A number of other AutoHotkey commands use Label names to initiate subroutines. Chapter Six explains how to launch subroutines with the MenuSetTimerOnExit, and GUI (gLabel) commands.

To develop a better understanding of the unique properties of Label names, Chapter Seven drives home the fact that they merely act as signposts in the script—not executable code. I explore unique ways to use Label names to 1) change parameters for a Hotkey one-the-fly; 2) run the initial setup routine and 3) create an automatic timeline for a series of steps or instructions (e.g. Jack Stuffed Cheeseburger recipe).

While the online AutoHotkey documentation discourages using the GoTo command, you can put its unique properties to work in certain types of scripts. In Chapter Eight, the unique properties of the GoTo command create a new Jack Stuffed Animal Style Cheeseburger from two parallel recipes.

AutoHotkey Label names offer a unique drop-through behavior which can provide special Hotkey resetting features. In Chapter Nine, I use this Labelname characteristic to reset transparent windows which became impossible to see.

In Chapter Ten and Chapter Eleven, I show you how to write scripts which you can either run as standalone apps or easily combine unchanged with other apps in an omnibus script. I recommend these script designing techniques as best practices for all AutoHotkey apps.

In Chapter Twelve, a question from a reader prompts a demonstration of techniques for converting repetitive subroutines into user-defined functions.

The unusual behavior of #Directives causes a great deal of confusion for the new scriptwriter. Chapter Thirteen explains the difference between AutoHotkey commands and #Directives and shows when to use each.

By the time you reach the end of this book, you should achieve enough of an understanding of how AutoHotkey processes and runs a script, plus the critical component parts found in AutoHotkey scripts, to write robust, easy-to-debug-and-alter applications. The online AutoHotkey documentation—although excellent—does not make it clear how AutoHotkey does its job. This book sheds light on the inner workings and hidden mechanisms of AutoHotkey while showing you how they all work together.

2 thoughts on “Beginner’s Guide to How AutoHotkey Scripts Work

  1. how make i more loops in 1

    loop 1 every 2 min
    loop 2 every 1 min and
    loop 3 every 3 min

    loop 1
    MouseClick, left, 756, 723
    Sleep, 500
    loop 2
    MouseClick, left, 733, 828
    Sleep, 500
    loop 3
    MouseClick, left, 665, 800
    Sleep, 30500


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s