TSolo315 MturkCrowd
Six years of turking experience

If Statements

Conditional expressions.

1

What is an "if statment?"


An if statement allows you to tell AutoHotkey to run a chunk of code only if a given condition is met (if it evaluates to True). This is very powerful, allowing you to significantly increase the versatility of your scripts. If statements follow the format if (expression) {code} with expression being the condition you are checking -- in the form of an expression, and code being the code you want to run if the condition is met. Here are a few simple examples:


if (followers > views)
{
    clipboard := "Too many followers"
    return
}


if (Color = "Blue" or Color = "White")
{
    MsgBox The color is one of the allowed values.
    return
}


if dividend { ; You can simply leave a variable name and if it evaluates to true the code will run.
  clipboard := "There is a dividend"
}

2

More on Booleans


When an expression is required to evaluate to true or false (such as an if statement), a blank or zero result is considered false and all other results are considered true. So the if statement if (5 + 7) would return true and the following code would run. If a variable doesn't exist it will return false. Operators like >, =, <, will will return true or false depending on the result.

3

NOT, AND, OR


There are a few special operators you can use with if statements:

  • The not operator checks for false instead of true. if not (Batches + Surveys = 20)
  • The and operator checks if all statements provided evaluate to true. if (apples = 20 and bananas = 20)
  • The or operator checks if ONE of the statements provided evaluates to true if (apples = 20 or bananas = 20)
4

Else and else if


Directly following an if statement you can have else if statements or an else statement. Else if statements will be checked if the original if statement condition was NOT met, and if no preceding else if statement conditions were met. Else statements are not followed by an expression and will run if the original if and all preceding else if statement conditions fail. Here is an example:


if (Color = "Green" or Color = "Cyan")
{
    MsgBox The color is one of the allowed values.
    return
}
else if (Color = "Gold") ; checked if initial if statement fails.
{
    MsgBox Gold is not an allowed color.
    return
}
else if (Color = "Silver") ; checked if initial if statement and preceding else if statement fails.
{
    MsgBox Silver is not an allowed color.
    return
}
else ; runs if all previous statements fail.
{
    MsgBox This color is not recognized.
    return
}

5

Practical Example.



f1::
click 2 ; Double click.
clipboard := "" ; Empty the clipboard.
sendinput ^c ; Copy highlighted text.
clipwait, 5 ; Wait for text to be copied.
result := clipboard ; Set result variable to clipboard contents.
if (result = "Painting" or result = "Song" or result = "Book")
{
  Category := "Artist"
}
else if (result = "Basketball" or result = "Soccer" or result = "Football" or result = "Tennis")
{
  Category := "Athlete"
}
else if (result = "Actor" or result = "Actress" or result = "Comedian")
{
  Category := "Performer"
}
else {
  Category := "NA"
}
sendinput ^w ; close tab.
sendinput {tab} ; send tab press.
sendinput %Category% ; send category variable.
return

Assume a hit asked you to visit the profile of a celebrity and return in what field they received their claim to fame. A script like this would allow you to hover over a keyword and click a hotkey, allowing AHK to handle the rest.

Next Tutorial: Loops