Variables vs. Constants

by Marjo Martinez, in Tech Talk, posted 5/18/07

A variable is a named storage location that can contain data which, if needed, may be modified during task execution. In this manner, variables can be used to represent and store dynamic data that may be different each time the task is run. Each variable has a name that uniquely identifies it. Variables can be used to represent data in a task that may be different from system to system.

How Variables Are Used
All variables to be passed into AutoMate's scripting language, either explicitly through the use of a 'BASIC script' action or implicitly through the use an Expression, need to always be created first. In other words, an initial step within the beginning of a task needs to create the variable before it can be recognized / utilized by any other steps. Once a variable is created, its contents can be displayed at runtime by placing the variable name in percent signs. The percentage signs (%) at the beginning and end of the variable name tell AutoMate that the text in-between the "%" is a variable (or expression), and should not be taken literally. Instead, it is replaced with the current contents of that variable at runtime. To use a variable, simply add a 'Create Variable' action from within the Task Builder.

How to Create a Variable
AutoMate's 'Create Variable' action generates a variable which can be used to store dynamic values for use in any step of the current task or any subtasks started with the 'Start Task' action. Once created, variables can be set using the 'Set Variable' action, which changes the contents of an already existing variable, or by certain actions that support populating variables (e.g. 'Input Box' action).

To grab data out of the variables in any action parameter or step, simply surround the variable name with percentage (%) signs. Remember not to use percentage signs when specifying the name of a variable to populate, percentage signs are only needed to get data out. Additionally, make certain that variable names used do not conflict with a BASIC keyword, function or instruction. A common way to avoid this is to insert your own text to the beginning of all variable names (e.g. "The" or "My"). So therefore, "Date" (which is a BASIC keyword) would become "TheDate" or "MyDate," thus avoiding any name conflicts.

Constants - Overview
A Constant is a variable whose value cannot be changed once it is initially bound to a value. In other words, a constant cannot be assigned to or cannot be modified during a task's execution. Although a constant value is specified only once, the value can, however, be referenced multiple times within a task. The value of a constant is considered "global" which means that it is available to all tasks on the system.

How to Create Constants
Before constants can be used, they must be created and configured in a particular location. Because constants are considered "global" they are configured from within the Task Administrator under 'System' -> 'Options' -> 'Constants' tab rather than within the steps of the task itself. In this manner, constants can be stored properly in the local machine's registry in the AutoMate 6 key. When creating a constant from the 'Constants' tab, the constants view is separated into three columns:

1. Name - the name of the constant. This is how the constant is referred to in AutoMate tasks.
2. Value - the value that is contained within the constant.
3. Comment - a description for the constant. This is used only in the Constant Options view as reference, and has no bearing on the constant itself.

Like variables, a constant name must begin with a letter, and contain only letters and numbers. Note that if removing a constant, you must make certain to remove or modify the steps of your tasks that referenced that constant. Tasks that refer to a constant that is not present on the system will fail when run.

How to Use Constants
Similar to variables, constants may be used in any AutoMate step parameter by specifying the name of the constant surrounded by percentage (%) signs. Using a constant instead of specifying a value multiple times in a task can simplify code maintenance. Additionally, constants can act to make tasks more portable, as they can easily convey information to the task at runtime about the current environment.

AutoMate tasks may be designed on one machine and run on others - either by exporting and importing or by deploying a task via remote administration. Because of this, sometimes it is necessary to specify a path to a file or any other piece of information that may be different from machine to machine. It is impractical to design a separate task for each machine. For instances such as this, AutoMate allows the use of constants.

For example:

If Machine "A" has Microsoft Office installed in C:\Office and Machine "B" has Microsoft Office installed in D:\MSOffice, when adding a 'Run' action, you could specify a defined global constant followed by the .EXE name in the path (e.g. %OFFICEFOLDER%\winword.exe). When the task is run on the target machines, AutoMate resolves the value at runtime to the appropriate value - which in this case is the directory of Microsoft Office (without the trailing backslash).