Syntax for Assignment and Comparison in AutoHotkey
Commands vs Expressions
Assignment:
Command assignment =
Everything to the right of the = sign is considered literal text.
Variables to the right must be enclosed in percent signs.
%MyVariable%
The = sign cannot perform calculations.
Command assignments are never necessary. They are merely convenient for assigning literal text because double quotes can then be omitted.
Expression assignment :=
Everything to the right of the := operator is considered an expression.
Literal text must be enclosed in double quotes.
"literal text"
Expressions can perform calculations.
You may intersperse literal text with variables and calculations using the := operator.
For example:
Code:
nam := "Expression 1:"
var := nam "two plus two equals" 2+2 "."
MsgBox, %var%
The contents of
%var% will be:
Expression 1:two plus two equals4. Note that since I did not add enough spaces into the literal text or use the
A_Space variable in the expression, the output is not spaced correctly.
Note also that using the = sign to create the
nam variable would've been easier since I could've omitted the double quotes then.
Comparison:
Command comparison If var = value Everything to the right of the comparison sign is considered literal text.
Variables to the right must be enclosed in percent signs.
%MyVariable%Command If statements cannot perform calculations.
Command comparisons are never necessary. They are merely convenient for comparing literal text because double quotes can then be omitted.
For example:
Code:
If var = literal string
If var = 4
If var = %MyVariable%
If var = ;<--compare to blank value
If var ;<--boolean
Wrong: If var = 2+2 Expression comparison If (var = "value") Everything to the right of
If is considered an expression.
Literal text must be enclosed in double quotes.
"literal text"Expressions can perform calculations.
For example:
Code:
If (var = "literal string")
If (var = "4")
If (var = MyVariable)
If (var = "") ;<--compare to blank value
If (var) ;<--boolean
If (var = 2+2)
Note that numbers will not be mistaken for variables even if double quotes are omitted, so the following works fine:
Code:
If (var = 4)
For this reason, it is usually unwise to use numbers as variable names.