Example

The following code fragment prompts the user for input. If the input string is the string true, the code fragment writes the message You typed TRUE on the host screen. If the input string is any other string, the code fragment writes the message You typed FALSE. This example uses the following actions: Prompt action, Condition action, and Input action.

You can copy this code fragment from this document into the system clipboard, and then from the system clipboard into the source view. Because this code is a fragment, you must copy it into a macro screen in an existing macro script. You must also create a string variable named $strData$. To create the variable, add the follow lines after the <HAScript> begin tag and before the first <screen> element:
<vars>
   <create name="$strData$" type="string" value="" />
</vars>
After you save the script in the macro editor, you can edit it either with the macro editor or in the source view.
Notice the following facts about this example:
  • The example consists of one code fragment containing an <actions> element and the actions inside it.
  • The first action is a Prompt action that displays a message window and copies the user's input into the variable $strData$, without writing the input into an input field in the session window.
  • The first part of the condition action (the <if> element) contains the condition, which is simply $strData$.
  • Because $strData$ is a string variable in a boolean context, the macro runtime tries to convert the string to a boolean value (see Automatic data type conversion). If the user's input is the string 'true' (in upper, lower, or mixed case), then the conversion is successful and the condition contains the boolean value true. If the user's input is any other string, then the conversion fails and the condition contains the boolean value false.
  • If the condition is true, then the macro runtime performs the action inside the <if> element, which is an Input action writing the message You typed TRUE on the host screen. When all the actions inside the <if> element have been performed, the macro runtime skips over the <else> action and continues macro processing.
  • If the condition is false, then the macro runtime skips over the actions in the <if> element and begins performing the actions in the <else> element, which includes one Input action that writes the message You typed FALSE on the host screen. After performing all the actions in the <else> action, the macro runtime continues macro processing.
Figure 1. Sample code fragment showing a Condition action
<actions>
   <prompt name="'Type true or false'" description="" row="0" col="0"
               len="80" default="" clearfield="false" encrypted="false"
               movecursor="true" xlatehostkeys="true" assigntovar="$strData$"
               varupdateonly="true" />
   <if condition="$strData$" >
       <input value="'You typed TRUE'" row="0" col="0" movecursor="true" xlatehostkeys="true" encrypted="false"/>
   </if>
   <else>
       <input value="'You typed FALSE'" row="0" col="0" movecursor="true" xlatehostkeys="true" encrypted="false"/>
   </else>
</actions>

For more information, see <if> element.