Variables, commands and function stuff
Creating a variable
AutoHotkey is a scripting language, designed to make things easy and fast to be coded. Variables don’t need to be created, “declared”. They aren’t typed either. So you can use a variable and assign some text to it or a number, it doesn’t matter.
1 ; any AutoHotkey version 2 MyVar := "Hello!"
This simply creates a variable named
MyVar and assigns the string Hello! (without the quotes) to it. If this is put in the auto-execute section, it should automatically be available to the rest of the auto-execute section below and all labels. There’s more to be said about variable scoping when we deal with functions later.
Concat text, numbers and variables
If you want to concat text, numbers or variables in tradtional mode, you needn’t to anything (except leave a space between 2 percent signs).
In expressional mode, there’s the concat operator, which is simply a dot (
.). However, using it is optional, you may just leave a space in there.
1 ; any AutoHotkey version except AutoHokey v2 and AutoHotkey_H v2. 2 name := "Tim" 3 message = %user% is very clever! ; tradtional concat
1 ; any AutoHotkey version 2 votes := 12 3 result := "Votes for " . user . ": " votes ; using 2 explicit concats and 1 implicit
Referencing a variable
Referencing a variable is basicly easy. However, there’s one big pitfall beginners often have problem with, called traditional vs. expressional. This not only affects variables, but also strings.
We’ll start with traditional:
1 ; any AutoHotkey version except AutoHokey v2 and AutoHotkey_H v2. 2 MyVar = Hello! ; traditional assignment: using "=", no quotes 3 MsgBox The content of "MyVar": %MyVar% ; command (traditional) 4 My2ndVar = %MyVar% ; traditional assignment: % around a variable
As you see, usually when referencing a variable, it is delimited by percent-signs. Literal text does not need to be enclosed in quotes, if they are present, they’re taken as literal quotes. But that is not always the case.
In AutoHotkey v2 (and AutoHotkey_H v2), literal assignments (i.e. line 2 and 4) are not supported. Command calls are still supported, using traditional syntax.
When assigning a variable with
:=, you force the left side to be an expression. As expressions also appear in some other cases, it’s important to know how to deal with them.
1 ; any AutoHotkey version 2 MyVar := "Hello!" 3 IsLabel(MyVar) ; dummy function call using expressional version 4 My2ndVar := MyVar
As you see, in expressional mode variables are not enclosed in percent signs. Literal text must be enclosed in double-quotes (AutoHotkey v2 and AutoHotkey_H v2 also support single quotes).
commands & functions
In AutoHotkey, there are 2 ways of executing code: commands and functions.
As you saw above, commands (like
MsgBox) use the traditional mode by default, functions (like
IsLabel()) use expressional mode. Additionally, in function calls, parameters are enclosed in parentheses whereas they aren’t in commands.
1 ; any AutoHotkey version 2 user := "Tom" 3 MsgBox Hi %user%! ; command: no parentheses, no quotes, but %-signs 4 5 suffix := "_2" 6 IsLabel("AnyLabel" suffix) ; function: parentheses + quotes, no %-signs
forcing expression mode
You can force expressional mode for a command parameter with a single percent sign:
1 MsgBox % "This is a parameter in enforced expression mode."
But you can’t do the contrary for functions.
A special case of command parameters are those called InputVar or OutputVar (+ some exceptions with other names such as in SplitPath). Those are taken as variable names: if you pass e.g.
MyVar it won’t take the string MyVar but the variable
In AutoHotkey v2 (and AutoHotkey_H v2), you can decide for everything whether you want to use command or function syntax to call it.
[missing: about input and output vars in AHK v2]
To prevent you from doing the common traditional vs. expressional mistake, here’s a summary from the help file’s FAQ:
When are quotation marks used with commands and their parameters?Double quotes (") have special meaning only within expressions. In all other places, they are treated literally as if they were normal characters. However, when a script launches a program or document, the operating system usually requires quotes around any command-line parameter that contains spaces, such as in this example:
1 Run, Notepad.exe "C:\My Documents\Address List.txt"
When exactly are variable names enclosed in percent signs?Variable names are always enclosed in percent signs except in cases illustrated below:
1) In parameters that are input or output variables:2) On the left side of an assignment:
1 StringLen, OutputVar, InputVar3) On the left side of traditional (non-expression) if-statements:
1 Var = 123abc4) Everywhere in expressions. For example:
1 If Var1 < %Var2%
1 If (Var1 <> Var2) 2 Var1 := Var2 + 100