Add the New Action & Create the Code

Step 1: Add the New Action

Quantrix provides a Script Manager to help you organize the scripts in a model. We will use the Script Manager to add our new action. With Quantrix open, show the Script Manager by clicking the Scripting menu at the top of the window and then clicking Script Manager. A check mark appears next to the menu item, and the Script Manager tool pane appears on the left side of the application window:

Tip: You can also show the Script Manager by pressing Ctrl+Shift+M (Command-Shift-M on Mac OS X).

The Script Manager displays the default library for the current model. Add an action to this library by right-clicking on the Library1 item and selecting Add Action. A new action is created and added to Library1, and the action editor displays.

Tip: You can also add an action by clicking the add action button (the green "A") in the Script Manager toolbar shown on the left, or by choosing Add Action from the Scripting menu.

You may notice that the new action lacks the perform() and enabled()methods discussed earlier. In fact, these methods are present but are not shown in the default edit mode. Since an action always has perform() and enabled() methods, the default edit mode saves time and reduces clutter by adding an empty perform() and a default (always true) enabled() to every new action, then hiding everything except the body of perform(). When you edit an action in the default mode, you are writing the body of perform(). If you prefer the full code listing, you can switch to expert mode by selecting the Expert Mode option:

If you want to specify a non-default enable condition, you can edit the body of the enabled() method by clicking the Enable condition check box and typing the body of your enabled() implementation into the resulting text area.

If the Scripting Menu check box is enabled, this action appears as a menu item in the Scripting menu for easy access. Clicking the menu item invokes the action.

The box containing the text “Action1” stores the name of the action. Click in this box and change “Action1” to the more descriptive name “Parity Check”. To commit the name change, press Enter or click outside of the text box.

Step 2: Create the Code

To write the body of the action, click in the main text area and type (or paste) the following:

boolean isEven = |Matrix1::A1:B1|.value % 2 == 0

if (isEven)

alert("The value is even.")

else

alert("The value is odd.")

You will notice that a green border appears around the text area when you begin typing. This border indicates that your current edit is uncommitted. In this context, an edit to a script is a sequence of changes that collectively form a single undo level. If you enter three lines of code into an action, commit that edit, and then perform an undo, all three lines will be undone together. If you then perform a redo, all three lines will reappear.

As soon as you begin typing into the script editor you begin an edit, which you must eventually either cancel or commit. You can cancel by pressing the Esc key; you can commit either by clicking outside of the editor’s text area or by clicking the green check mark that appears in the upper right corner of the text area when you begin typing.  Below is an example showing the green commit check mark:

When you are finished typing the action body, click the green check box or click outside the text area to commit.  See the ParityCheck models in the Sample models folder (Samples >Scripting > Getting Started Guide folder).

See Also

Continue to Step 3: Test the Action & Add an Enable Condition