AutoIt Variables

How to declare and work with variables in AutoIt, as well as some background information.

5170 views
d

By. Jacob

Edited: 2021-06-05 12:25

AutoIt Logo

AutoIt makes it possible to store information in Variables, the information can then be accessed at a later time in the script.

In AutoIt, variables are created using the dollar sign ($), and the variable name can only contain letters, numbers, and underscore (_).

How to Declare Variables

Below is a variable being assigned with the default text-content "This is a Test". It is possible to assign a variable without declaring it.

$My_Variable_Name = 'This is a Test'

A variable like the above can be accessed later. A good way to test things in AutoIt, while learning, is to crease a message box to show the output or content of variables. We can show the content of the above variable with the MsgBox function. I.e.:

$My_Variable_Name = 'This is a Test'
MsgBox(1, "My Title", $My_Variable_Name)

It is generally good to declare variables before they are used. In AutoIt, you may declare variables using the Local and Global keywords. The keyword that is used to declare a variable, determines if it should be accessible globally (the entire script), or only locally (inside a given function). Multiple variables can be declared by seperating them by comma, saving you from having to re-type the keyword for each variable.

Global $username, $password

Joining variables and strings

When you want to join multiple variables together, you may use the and (&) sign. You can both use this for variables and strings.

$word1 = 'Your '
$word2 = 'friend'
$phrase = $word1 & $word2

To add even more variables, simply another ampersand followed either by a quoted string or another variable.

$word1 = 'Your '
$word2 = 'friend'
$phrase = $word1 & $word2 & 'String in Quotes'

Strings and integers

Some languages have different datatypes for variables, depending on whether they contain text or numbers (integers). A string usually just consists of text and other characters. But a string can also be an image, video or mp3 file, Etc. Of Course those would be "very large" strings, but it should make it clear that strings have many uses, besides containing just smaller text messages.

AutoIt only has a single datatype called a Variant. If you try to multiply two variants, they will be treated as numbers. And if you join two variables, they will be treated as strings.

Tell us what you think: