TSolo315 MturkCrowd
Six years of turking experience

Variable Basics

Saving and altering values

1

What is a variable?


A variable is a placeholder you can assign a value to. This value can be used or changed repeatedly as long as your script is running. Variables are assigned using the format myIntVar := 0. "myIntVar" is the variable name and "0" is the value. If the variable is going to hold text, the text has to be surrounded by quotation marks myStrVar := "applesauce". You can name a variable whatever you like, but ideally choose something that would make it clear what value the variable is meant to hold. In this tutorial I will use variations of "myVar." Variables can be sent using the sendinput command by being enclosed in percentage signs: sendinput %myStrVar%. The percentage signs lets AHK know you want to send the variable myStrVar and not the literal text "myStrVar."

2

Using Variables


Variables are a powerful tool that can really increase the flexibility of your scripts. They allow you to save and update information. For example, you can set multiple hotkeys, each set up to copy a piece of information to a variable, and then have a final hotkey set to fill a HIT with all of the information you have gathered into these variables. It might look something like this:


f1::
click 2
clipboard =
sendinput ^c
clipwait, 30
myVar1 := clipboard
return

f2::
click 2
clipboard =
sendinput ^c
clipwait, 30
myVar2 := clipboard
return

f3::
click 2
clipboard =
sendinput ^c
clipwait, 30
myVar3 := clipboard
return

f4::
sendinput %myVar1%
sendinput {tab}
sendinput %myVar2%
sendinput {tab}
sendinput %myVar3%
return

The hotkeys f1, f2, and f3 copy the word under the cursor and saves it to a unique variable. The hotkey f4 then writes the saved words into a form, with a tab press between each word. When setting text from the clipboard (which is a built in variable) to a new variable you do not need to use quotes.

3

Clearing Variables


It is often a good idea to clear a variable after use to avoid accidents involving variables holding old information. To do so you would simply use the format myvar := "". Adding to the previous example, clearing the variables after using them with the f4 hotkey would look like this:


f4::
sendinput %myVar1%
sendinput {tab}
sendinput %myVar2%
sendinput {tab}
sendinput %myVar3%
myVar1 := ""
myVar2 := ""
myVar3 := ""
return

4

Counters and Incrementing


You may likely find yourself in a situation where you need to use a counter to count the number of times something happens. This is as easy as creating a variable, setting the value to zero (myVar := 0), and whenever you want to add one to the counter use the format myVar++. This is shorthand for myVar := myVar+1 which is a simple expression adding one to the value of your variable. We will go over expressions in a separate tutorial.

5

Strings


In programming text is often referred to as a "string." In AutoHotkey you can use variables to manipulate strings, such as adding two pieces of text together or replacing a piece of text with something else. In this tutorial we will simply cover adding two strings together. This can be done simply by setting each string to a separate variable and then adding them together using the format MyNewVar := MyVar1 MyVar2 or MyNewVar = %MyVar1%%MyVar2%. You can add unique text between the text stored in the two variables by doing something like MyNewVar := MyVar1 " can be found at: " MyVar2.

Next Tutorial: Clipboard and StrReplace