IF Commands


Allows conditional execution of blocks of steps. There are two types if IF statements in vTask:

1) Normal IF Block:
This is the standard implementation of the IF statement, which consists of the IF check, a block of code, and an END IF statement. An optional ELSE statement may also be included. "IF...ELSE IF...ELSE...END IF" blocks are functionally identical to the processing of all computer languages, such as BASIC.

IF expression = true
    command block
END IF


Another Example, using ELSE:


IF (expression)
    command block (if true)
ELSE
    command block (if false)
    (the ELSE block is optional)
END IF


Another Example, using ELSE IF:

IF expression #1
      command block
ELSE IF expression #2
      command block
ELSE IF expression #3
      command block
ELSE
      command block
END IF

2) Quick Check:
This option provides a single-line IF that can be used as a checkpoint, so no "END IF" or "ELSE" statement is needed or allowed. The "On Failure" selection is performed when the IF check evaluates to FALSE. If the evaluation equates to TRUE, no action is taken (ie, it passed the checkpoint). All standard Failure Modes are supported (show error, stop run, etc).

The "Quick Check" concept is to provide an easy method to validate items at a point in the script, without needing to add a whole "IF...END IF" block. The FALSE evaluation is used to generate action (instead of TRUE), because vTask implements a full selection of Failure Mode options (the default TRUE action is to continue processing).

The Quick Check provides functionality similar to the common programming construct:

IF (expression) = FALSE THEN [failure action]



For any of the IF checks, you may specific it to use IF NOT logic. This option has the effect of inverting the check for success. Programmatically, this would appear as:

IF NOT (expression which fails) THEN [success]

More Examples:

"IF File Exists" + IF NOT:
       (file does not exist yet) = [success]


"IF File Exists" + IF NOT:
       (file exists) = [failure]


IF blocks can be nested to multiple levels:



*Tip:  Be sure to use the Auto-Indent and Validate command with scripts that use Loops and IF statements. It will help your script be more readable and reliable.


*Tip:  There is an abbreviated way to do an IF...THEN...ELSE block in a vTask script, which helps to minimize steps. Programmers may be already familiar with this trick, which is called a "Ternary conditional." A ternary operation has the basic structure "IF condition ? if true : if false".

Putting that into practice, when you have a block of steps that looks like this:



They can be combined into a single step, like this:



A reduction from 5 steps into just 1! That's definitely a great optimization, especially when clarity is not sacrificed. For more information, refer to the Excel-compatible IF conditional in vTask.


The available commands are:


  IF Expression
  IF Text
  IF Variable
  IF File Exists
  IF Folder Exists
  IF Window Exists
  IF Key is Pressed
  IF Caps Lock is On
  IF Pixel Equals
  IF Pixel is Visible
  IF Image is Visible
  IF Clipboard Contains
  IF Internet
  IF Registry Key Exists
  IF Process is Running
  ELSE
  END IF


*Tip:  NOTE: "ELSE IF" is contained as an option inside of each "IF" check (meaning that any IF check can be turned into an ELSE IF check)



  IF Expression
 

Conditionally executes a block of steps, if the specified expression evaluates to TRUE. Math expressions or text comparisons can be done.

The IF NOT option will allows you to do a reverse check, which means that the IF will succeed if the check evaluates to FALSE. Conversely, the IF NOT check will fail if the result is TRUE.
This is commonly called a "IF NOT" check in programming. The advantage to this option is the ability to run a block of steps without needing to place the steps in the ELSE section.

Multiple parts can be included in the expression, using || and &&. This allows more than one expression to be evaluated. See below for examples.

*Tip:  To compare two text strings, use the IF Text action instead of IF Expression. IF Text includes many text-specific options, and also is necessary for comparisons if the text contains embedded operator characters such as >, <, or =.

For more information concerning Expressions, please refer to the help topic Running Scripts - Expressions.

Examples:

ExpressionEvaluates To:
%foo = 1 True if %foo is set to 1
%foo = hello world True if %foo is set to the text "hello world"
(1+2/3) >= (4+5/6) False
(see Math Calculations for more examples of math functions.)
test != Test
test <> Test
True
(see Expressions for more examples of equality testing.)
1 = 2
(with IF NOT option)
True
1 || 2 True
0 || 1 True
1=2 || 2=3 False
1=2 || 2=2 True
1 && 0 False
1=1 && 2=2 True
1 && 7 && 2 True
1 && 0 && 2 False
hello = goodbye || yes = yes True
%foo = 10 || %foo = 20 True
hello = goodbye && yes = yes False
1 || 7 && 2 Multi-part expression with
mixed operators are not supported
in the current version of vTask.



  IF Text
 

Conditionally executes a block of steps, if the specified text string comparison evaluates to TRUE. Variables or text prepared ahead of time can be used for both the main source string and the comparison.

The two text strings have no limit when variables are used, however if you use a prepared string for the comparison value (string #2), it will be limited to 255 characters. Use variables for both the source and comparison if you need unlimited length.

Examples:

StringCheck TypeComparison ValueResult
  abc123   Has Any Value (default) True
abc123 Is Empty   False  
abc123 Equals Exact Value abc123 True
abc123 Equals Exact Value abc1 False
abc123 Equals Value Insensitive ABC123 True
abc123 Starts with ab True
abc123 Ends with 23 True
abc123 Length Equals 6 True
abc123 Length Equals 7 False
abc123 Length Less Than 10 True
abc123 Length More Than 10 False
abc123 Contains Exact Substring bc True
abc123 Contains Exact Substring BC False
abc123 Contains Substring (Insensitive) BC True
abc123 Contains Any Characters from List abcdefg True
abc123 Contains Any Characters from List xyz False
abc123 Contains Only Characters from List abcdef123456 True
abc123 Contains Only Characters from List abcdef False



  IF Variable
 

Conditionally executes a block of steps, if the specified check of a variable evaluates to TRUE.

This check evaluates the state of a variable, not its value. To perform comparisons on the actual value of a variable, such as "%foo > 3" or "%foo = Hello", use the IF Expression or IF Text checks.

Variable CheckDescription
If Defined (Exists) Returns TRUE if the variable has been previously defined. This is often referred to as checking if the variable is not NULL. This will return TRUE even if the variable is empty. Note that this can return TRUE if the variable was defined in another script, and the "Reset user variables before each run" is turned OFF.
If Empty Returns TRUE if the variable has been previously defined, but contains no value. This will fail if the variable does not exist.
Has Numeric Value Returns TRUE if the variable contains only numbers or the characters [space], [-], [,], or [.]



  IF File Exists
 

Conditionally executes a block of steps, if the specified file exists. Wildcards (*, ?) can be used as a file match specification. If there are no files that match the specification, then processing continues with the matching IF action, or the step generates an error if it's a Quick Check.

This is a good candidate for using the IF NOT option - you can do something if the file doesn't exist yet (without needing to locate the steps in the ELSE section).


  IF Folder Exists
 

Conditionally executes a block of steps, if the specified folder exists. If there are no folders that match the specification, then processing continues with the matching IF action, or the step generates an error if it's a Quick Check.


  IF Window Exists
 

Conditionally executes a block of steps, if the specified window exists (based on the window title). For a description of the Window Match types, see the "Use Window" action.

If there are no windows that match the specification, then processing continues with the matching IF action, or the step generates an error if it's a Quick Check.


  IF Key is Pressed
 

Conditionally executes a block of steps, depending on the pressed status of a given key. You can check for an alphanumeric letter, like 'A' or 'z', or a system key like {Escape}.

The Press Type is the method to use to determine keypress. "Right Now" requires the key to be physically pressed when the check occurs, and "Since Last Check" will pass if it was pressed at any time since the step was last run.

If the key button is not toggled, then processing continues with the matching IF action, or the step generates an error if it's a Quick Check.


  IF Caps Lock is On
 

Conditionally executes a block of steps, if the CAPS LOCK keyboard button is activated. This does not check the state of the SHIFT button, nor does the SHIFT button affect the status of this action. If the Caps Lock button is not toggled, then processing continues with the matching IF action, or the step generates an error if it's a Quick Check.


  IF Pixel Equals
 

Checks a specific pixel on the screen, and conditionally executes a block of steps, if the pixel is equal to the specified RGB color. This check is useful as a fast alternative to image-based IF checks.

The desired color should be in "Red-Green-Blue" hexadecimal format (same as HTML colors). Examples: #FFFFFF (white), #808080 (gray), #000000 (black), #0000FF (blue).

If the pixel does not match the specified color, then processing continues with the matching IF action, or the step generates an error if it's a Quick Check.


  IF Pixel is Visible
 

Conditionally executes a block of steps, if the specified pixel is found anywhere on the screen.

The desired color should be in "Red-Green-Blue" hexadecimal format (same as HTML colors). Examples: #FFFFFF (white), #808080 (gray), #000000 (black), #0000FF (blue).

If the pixel color cannot be found, then processing continues with the matching IF action, or the step generates an error if it's a Quick Check.


  IF Image is Visible
 

Conditionally executes a block of steps, if the specified image is found on the screen. If the image cannot be found, or is covered by another window, then processing continues with the matching IF action, or the step generates an error if it's a Quick Check.

For more information, see the Using Images section.


  IF Clipboard Contains
 

Conditionally executes a block of steps, if the specified text matches the current clipboard contents. The match options are: Exact, Partial/Case Insensitive, Is Empty, and Any Text.
If the match type is Exact, the text must completely match in length, capitalization, and contents. If the match type is Partial, it is successful if any portion of the text is equal, even if the capitalization does not match. "Any Text" could also be described as "Is Not Empty". If the clipboard contents do not match the specified text, then processing continues with the matching IF action, or the step generates an error if it's a Quick Check.

An alternative method to evaluate the clipboard contents is to use the {clipboard} system variable.


  IF Internet
 

Performs a number of different checks on web links.

Check TypeDescription
Connected Checks to see if an the computer has a valid and active internet connection. If the computer is not connected, this command will not cause it to make a connection. This is different from the Ping command in that it actually checks for live web access, instead of verifying an IP address.
Valid Link Confirms a web URL (address) for existence. A "404" (Not Found) error, as well as other errors, will make this return FALSE. Use this option to confirm that a web link is correct and active.



  IF Registry Key Exists
 

Checks to see if the specified registry key exists. To check for a key or value, enter a key name such as:

HKEY_CURRENT_USER\SOFTWARE\vTask\AutoReload


To check for a tree entry (the items on the left side of RegEdit), be sure to include a suffix "\", such as:

HKEY_CURRENT_USER\SOFTWARE\vTask\


For more information concerning creating and modifying registry keys, see the Registry Commands in the System Actions.


  IF Process is Running
 

Checks to see if the specified program is running. The program does not need to be visible for the check to succeed. This action can check for an EXE file, such as "notepad.exe", or a Process ID such as "548".

For more information concerning Windows Processes, see the Process Loop action.


  ELSE
 

Executes a block of code following an IF action that evaluated to false. Execution will continue to the next matching END IF step.

*Tip:  The ELSE action is not required in an IF block. It is only necessary if you need to execute alternative steps when an IF evaluates to false.

*Tip:  NOTE: "ELSE IF" is contained as an option inside of each "IF" check (meaning that any IF check can be turned into an ELSE IF check)


  END IF
 

Indicates the end of an IF block.

*Tip:  The END IF action is always required when using an IF statement. If it is not included, vTask will still execute the script, but the results may not work as expected.

When the matching END IF statement is not found, vTask will display a warning message in the Output window. The following script illustrates an unclosed IF statement:



[STEP 1]: IF Expression "1=1"
[STEP 2]: Display Message "asdf"
[STEP 3]: ELSE
        WARNING: Matching ELSE/END IF statement not found!