Example

The following code fragment prompts the user for input. If the input string is the string true, the code fragment displays a message window with the message "You typed TRUE". If the input string is any other string, the code fragment displays a message window with the message "You typed FALSE". This example uses the following actions: Prompt action, Condition action, and Message action.

You can copy this code fragment from this document into the system clipboard, and then from the system clipboard into the Code Editor. 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 with the Code Editor.
You should 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 a Message action displaying the message You typed TRUE. Then, having exhausted all the actions to be performed when the condition is true (just one action here), the macro runtime skips over the <else> action and continues as usual.
  • In contrast, 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 include one Message action that displays the message You typed FALSE. After performing all the actions in the <else> action, then the macro runtime continues as usual.
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$" >
       <message title="" value="'You typed TRUE'" />
   </if>
   <else>
       <message title="" value="'You typed FALSE'" />
   </else>
</actions>