# Text mode for Scripts

## About Text Mode

Using Text mode, you can set up the same scripts as Block mode scripts. Any operations supported by the screen editing software in Block mode scripts is also supported by Text mode scripts, such as mathematical operations (sine, cosine, etc.), logical operations (&&, ||, etc.) and comparison operations (==, !=, etc.). Scripts that are not listed in [Toolchest] are not supported.

### Script Editor

In Text mode, the script editor divides the script into sections, with each script section having a trigger and one or more operations.

Triggers, such as Touch, Periodic/Schedule, and Conditional, take the form of a function. If a trigger requires parameters, pass them as function parameters.

Operations take the form of a statement with a semicolon (;) to end the statement.

The Text mode format is as shown in the following example.

- 'onClick' is the trigger handler for a Click trigger.
- The first parameter '$GraphicObject' is the source of the trigger.
- The second parameter '$Math.max(EXPRESSION1, EXPRESSION2);' is the set of operations that are executed by the trigger. The operations are identified by the Arrow '=>'.

#### Objects and Variables

Add a keyword to identify a referenced object or variable. Prefix the keyword with the '$' symbol. (Refer to the following table.)

Module Identifier

Keyword | Description | Example
$Alarm | To access an alarm property. | $Alarm.AlarmGroup1.Var1_HiHi.Message
$AlarmGroup | To access an alarm group property. | $AlarmGroup.AlarmGroup1.CurrentActiveCount
$AllAlarm | To access a property of all alarms. | $AllAlarm.MaxRecord
$SystemErrorGroup | To identify as system error group. |
$OperationLog | To access an operation log property. | $OperationLog.ExportData.FolderName
$RecipeControl | To access a recipe control property. | $RecipeControl.RecipeControl1.RecipeID
$Ingredient | To access an ingredient property. | $Ingredient.RecipeFamily1.RecipeGroup1.Ingredient1.Value
$Local | To access a local variable property. | $Local.Var1.Value
$GraphicObject | To access a graphic object property. | $GraphicObject.NumericDisplay1.CurrentValue
$Screen | To access a screen property, or to access a property of an item on a specific screen. Use this identifier to avoid any potential screen/content and object naming conflicts. | $Screen.Screen1
$Device | To access a driver or equipment property. | $Device.Driver1.Equipment1.OnScanMode
$Target | To access a target property. | $Target.Preferences.ColorSetID
$Math | To access math operations. | $Math.abs(-100)
$Enums | To access operations and values you can specify with scripts, such as file operations and data formats. | $Enums.FileOperation.CopyPaste $Enums.DataFormat.Float

By referencing objects and variables in a script, you can work with an object's or variable's properties. For example, 'Var1.Quality', '$GraphicObject.Ellipse1.Thickness'.

When you type the name of a graphical object, variable, alarm group, user group, logging group, or recipe in a script, the name is highlighted.

#### Changing Script Mode

In the script editor, you can use either [Block] mode or [Text] mode. You cannot edit scripts in both modes at the same time.

The default is [Block] mode.

To change the script mode, go to [Project] ➞ [Settings], then in the [Properties] window ➞ [Basic] ➞ [Script] ➞ [Script Mode], and select either [Block] or [Text].

Note: You cannot change the Script Mode if any of the script is invalid. However, you can change the Script Mode even if there is a data error.

## Working with Scripts in Text Mode

### Adding Functions

When working with scripts in Text mode, the Toolchest lists triggers, operations, logic, and expressions. Double-click a trigger and a new script section is created in the script editor along with the default code for the selected trigger.

You can also add functions using the following methods.
You can add a trigger from the tool bar.

- To edit a brand new empty script in Text mode, from the tool bar add [Blank] or use the keyboard shortcut Ctrl+Shift+N.

- To create a script that is triggered by an object on a screen, open the screen, select the object and the [Events] window lists the triggers available for the object. Click [+] to add the associated trigger in a new script section.

### Adding Source and Target

- In Text mode, the script editor is equipped with Intellisense. As you type in the script editor, available matching options pop up, whether it be a script keyword, object name, or function name. Select the desired item to insert the keyword, object name, or function.
  After typing an object name and a period (.) available object properties pop up. Select the desired property to insert it in the script.

- In Text mode, you can also create local and global variables on the fly. Other objects cannot be created in a script.

1. When an undefined variable is used in a script, hover the mouse cursor over the variable.

   Note: When creating a local variable, prefix the variable name with "$Local".

2. Click the icon and from the popup menu select either [Create variable] or [Create local variable].

3. From the submenu select the variable data type to create the specified variable.

### Toolchest

- In the Toolchest, click </> to see the syntax for the associated script.

### Comments

In Text mode, you can add comments to scripts with double slashes "//" and you can add multi-line comments in between "/*" and "*/". You can add comments at the end of a statement or on its own line.

Note:

- When changing the [Script Mode] from [Block] to [Text] mode, Comment blocks are converted to "//" comments.

- When changing the [Script Mode] from [Text] to [Block] mode:

- "//" and "/*" "*/" comments at the Trigger level are converted to Comment blocks.

- Other "//" statements or "/*" "*/" located outside the Trigger level are not converted as Block mode supports [Comment] blocks in Triggers only.

### Find and Replace

- Find: Use the keyboard shortcut key Ctrl+F to display the Find control in the top-right of the Text mode script editor. In the field, type the text to search for. Click the down or up arrow to find the next or previous match. Select the or icon to find the matching case or matching whole word.

- Replace: Use the keyboard shortcut key Ctrl+H to display the Find and Replace control in the top-right of the Text mode script editor. In the fields, type the text to search for and the replacement text. Click the down or up arrow to find the next or previous match. Select the or icon to find the matching case or matching whole word. Click the Replace icon to replace the current match. Click the Replace All icon to replace all instances of matching text.

The following table lists shortcut keys for Find and Replace operations.

Shortcut icon | Shortcut keys | Description
– | Ctrl+F | Find
– | Ctrl+H | Find and Replace
| F3 | Find next
| Shift+F3 | Find previous
| Alt+R | Replace
| Alt+A | Replace all
| Alt+C | Match case
| Alt+W | Match whole word

You can toggle between Find and Replace, either entering the shortcut keys or clicking the up or down icon to the left of the [Find] field.

## Restrictions

#### Script Mode

- You cannot change the Script Mode (Block mode to Text mode or Text mode to Block mode) if any of the script is invalid. However, a data error will not prevent you from changing the Script Mode.

- Block mode scripts support comments only in the Comment block of a Trigger block. As a result, in Text mode, comments added to any area of the script other than the Trigger are not saved when changing the [Script Mode] to [Block] mode.

#### Names

- If an object is either deleted or renamed in the project, references to that object in scripts appear as an undefined error.