AutoHotkey Tip of the Week: Understand How Hotstring Activating Text Works

Sometimes We Limit Our Scripts by Not Grasping How AutoHotkey Features Respond

In AutoHotkey, we use Hotstrings for automatic text expansion and replacement—as in the extensive list of common misspellings found in the AutoHotkey AutoCorrect.ahk script. After loading this series of Hotstrings, if you misspell one of these words, AutoHotkey instantly corrects it. I’ve included this as a standard part of my main AutoHotkey script and often watch the Hotstrings in action when they correct one of my typos or misspellings.

Beginning AutoHotkey Hotstrings 200pxWhenever a Hotstring fires, it resets and waits for the next one. Most commonly, this occurs when we type a space, period or other punctuation as a Hotstring recognizer. One might fall prey to the misconception that every such space or punctuation key press causes a Hotstring reset, but not so. Hotstring monitoring only resets when a Hotstring event occurs (or with a mouse click or cursor movement). That means we can include spaces and punctuation in the activating strings (or Hotstring definitions) which appear after the first double-colon.

Light Bulb!Most AutoHotkey users might understand this fact about Hotstring activators, but I write about it here for those of us who either never fully realized the importance of this aspect of Hotstrings or we just forgot. Knowing this fact allows for a number of additional types of Hotstrings rather than limiting ourselves to standard text expansion and replacement. A perusal of the AutoHotkey AutoCorrect script lets us peek into how we can take advantage of this aspect of Hotstrings. Continue reading

AutoHotkey Tip of the Week—Powerful RegEx Text Search Shorthand (~=)

AutoHotkey Provides an Abbreviated Regular Expression RegExMatch() Operator ( ~= ) for Quick Wildcard Text Matches

Regular Expressions (RegEx) can get confusing, but once understood, they pay tremendous dividends. Acting almost as another programming language, Regular Expressions in AutoHotkey provide a method for accomplishing complex search and/or replacement with only one line of code. While not impossible, doing the same thing without using RegEx often requires complex tricks and many lines of code. In the beginning, learning RegEx many feel daunting but you’ll find it well worth the journey.

Light Bulb!In spite of the initial learning curve, you don’t need to learn how the two primary AutoHotkey RegEx functions work (RegExMatch() and RegExReplace()) to make good use of a RegEx. The shorthand RegEx operator ( ~= ) provides a method for doing a complex string match without the limitations of the InStr() function. Regular Expressions search for patterns while the InStr() function searches for exact strings.

Continue reading

AutoHotkey Tip of the Week: Embed Images Directly in the AHK Script

Rather than Use the FileInstall AutoHotkey Command, This Trick Allows You to Lodge Images Directly into Your AutoHotkey Scripts

Light Bulb!In the past, whenever I wanted to add a graphic image to an AutoHotkey script, I needed to either separately provided the file or embed it in the compiled EXE file using the FileInstall command. The AutoHotkey world has opened my eyes to an ancient technique for embedding an image directly in the AHK script—no need to supply the file separately or embed it by compiling the script. Continue reading

AutoHotkey Tip of the Week: Add Power to Any Command with Forced Expressions (%)—October 7, 2019

Use the Forced Expression Operator (%) to Adapt AutoHotkey Commands On-the-Fly

Light Bulb!Most AutoHotkey Version 1.1 commands use plain text as parameters. That means AutoHotkey interprets the text exactly as written—no quotes needed and no variables accepted. In version 1.1, this would limit the commands except for the availability of the traditional (or legacy) double-quote text replacement operator (%var%).

The %var% technique allows us to substitute any variable directly into an AutoHotkey command. However, using the forced expression operator (%) adds even greater flexibility and power to almost any AutoHotkey code. In many cases, rather than developing complicated subroutines, you can use this technique to embed expressions and functions directly into version 1.1 AutoHotkey commands. Continue reading