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.


Note: The alpha version of AutoHotkey 2.0 drops the forced expression operator (%) and modifies the traditional variable replacement (%var%) to (%expr%) primarily for use with pseudo-arrays (array%number%) where the variable number evaluates to a portion of the pseudo-array element name. Version 2.0 replaces commands with functions, therefore the function parameters accept variables and expressions—no longer needing to force expressions. For more on the differences between AutoHotkey versions 1.1 and 2.0, see A Peek at the Coming AutoHotkey V2.0.


In Chapter Seventeen, “Force an Expression (%) in AutoHotkey for More Powerful Commands: Learn the Secret of Adding Power and Flexibility to AutoHotkey Commands—Use Forced Expressions to Tailor Almost Anything” of the book AutoHotkey Hotkeys: Tips, Tricks, Techniques, and Best Practices for Automating Your Windows Computers, I show how forced expressions (%) expand the capabilities of AutoHotkey coding. While we may remember how forced expressions work, we often forget to use them.

How Forced Expressions Work

By placing the forced expression operator (%) in front of a traditional text parameter, AutoHotkey evaluates it as an expression (i.e. a constant turns into a variable). Without the forced expression operator %, we would only have the double percent sign (%var%) substitution available for tailoring commands in AutoHotkey version 1.1.

You’ll find plenty of information about how to force an expression with numerous examples throughout the online documentation, but the contents do not emphasize its deeper importance. The Variables page of the online documentation offers a number of uses for the technique:

FileAppend, % MyArray[i], MyFile.txt
FileAppend, % MyPseudoArray%i%, MyFile.txt
MsgBox % "The variable MyVar contains " . MyVar . "."
Loop % Iterations + 1
WinSet, Transparent, % X + 100
Control, Choose, % CurrentSelection - 1

You’ll find no one place reminding you to investigate this technique whenever you need to change a static command into a dynamic tool.

As an example, a SplashImage command line from Chapter Thirteen, “AutoHotkey Scan Codes, Speech, Sound, and Splash Images in Children’s Apps” of the same book offers only one image option as written in the NumbersSpeak.ahk script:

SplashImage, cow-skating.jpg, cwYellow ctBlue 
    , C IS FOR COW!, Is the cow laughing?
    , Laughing Cow

(I wrapped this one line of code into multiple lines for display purposes using AutoHotkey line continuation techniques. AutoHotkey reads it as one continuous line.)

But, what if we want to switch between two different images each time the SplashImage command runs. By replacing the image filename from the example above with a forced expression using a toggle with the ternary operator, the image switches automatically each time you run the command:

SplashImage, % (toggle := !toggle) 
 ? "cow-skating.jpg" : "Dancing Dogs.jpg"
 , cwYellow ctBlue
 , C IS FOR COW!, Is the cow laughing?
 , Laughing Cow

You can also do this for each of the other three parameters shown (i.e. “cwYellow ctBlue”, “C IS FOR COW!, Is the cow laughing?”, and “Laughing Cow”).

Rules for Forcing an Expression in AutoHotkey Commands

Considerations when using forced expressions in AutoHotkey commands:

  • The percent sign % must be the first character in the parameter and followed by a space or tab.Library Benefits

    The expression must account for the entire space between any two commas. The comma is an operator. When AutoHotkey encounters a comma, it marks the end of the expression. (Any comma inside a forced expression must be enclosed in quotes.)

  • Any text must be enclosed in quotes and merged with variables and other text using the concatenate operator (dot surrounded by spaces),
  • Forced expressions will not work within commands which already evaluate expressions (e.g. If (expression) statements). Generally, if parentheses contain an expression, trying to force an expression with % throw an error.
  • Forced expressions are not accepted as output variables (e.g. OutputVar) in AutoHotkey commands.

You can use the forced expression operator for most command parameters separated by a comma.

Variable Evaluation (% Expression) Versus Variable Text Replacement (%Var%)

For additional examples and explanation of the difference between forced expressions (%) and variable replacement (%var%), see Chapter 3.1.2 “Window Mouse Transparency Wheel” subtitled “Handy Window Transparency Wheel Using Macro Replacement Quickly Peeks Under a Window without Moving It, Plus the Difference Between % Var and %Var% Made Easy, in the Motley Assortment of AutoHotkey Tips book.

WinSet, Transparent, % %currentWindow%, ahk_id %currentWindow%

The WinSet command above demonstrates how to use the variable replacement (%var%) with a forced expression (%) to create a dynamic command changing its target with each new active window. See the SeeThruWinWheel.ahk script for this variable within a variable command in context.

Click the Follow button at the top of the sidebar on the right of this page for e-mail notification of new blogs. (If you’re reading this on a tablet or your phone, then you must scroll all the way to the end of the blog—pass any comments—to find the Follow button.)


This post was proofread by Grammarly
(Any other mistakes are all mine.)

(Full disclosure: If you sign up for a free Grammarly account, I get 20¢. I use the spelling/grammar checking service all the time, but, then again, I write a lot more than most people. I recommend Grammarly because it works and it’s free.)

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

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 )

Google photo

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

Twitter picture

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

Facebook photo

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

Connecting to %s