Use Script Objects

As an application designer or story developer, you can use script objects to encapsulate a set of functions that can be reused in scripting.

About Script Objects

A script object is a non-visible part of an analytic application or optimized story, which groups a set of functions. You can use it directly in event handlers. This keeps you from duplicating codes and makes maintenance of applications or stories easier.

Once you've added a script object, you can write scripts for it, which act as a script function. Each script function has the following elements:
  • name

  • return type

  • arguments

You can access the script object in scripts by entering its name like other widgets. Inside the function of a script object you have access to all objects in the application or story such as widgets, global variables and popups.

Create a Script Object and Script Function

Procedure

  1. In the Scripting area of the Outline panel, (for analytic applications) choose right next to Script Objects, or (for optimized stories) choose Start of the navigation path Next navigation step  Script ObjectsEnd of the navigation path.

    The new script object is displayed under Script Objects, and a Script Function panel opens.

  2. In the Properties section, configure the following settings for the script function:

    Element

    Description

    Name

    The name is mandatory and it validates according to the same rules as for global variables. Name can't be empty or a reserved keyword and should begin with a letter.

    Description

    (optional) A brief description of the function.

    Return Type

    Can be void, string, boolean, integer or number. By default, the return type is void.

    Besides the primitive types described above, you can also select a wide variety of non-primitive types like Button, Category, Chart, Clock, DataSource, Table, UrlType.

    Set as Array

    Only activated if the return type is not void.

    Note

    All modifications take effect immediately without your having to select Done, for example, after you press enter on an input, change the dropdown selection or switch to an array.

  3. In the Arguments section select to create a new argument for the function.
  4. In the Argument panel, edit name and type of the argument.

    If the argument should represent a list of this type, set it to array.

  5. Choose Done to close the Argument panel and return to Script Function.
  6. Optional: You can reorder the arguments by dragging and dropping them inside the panel.

    This changes the order of the arguments in the defined function.

  7. In the Outline panel select (Edit Scripts) right next to the script object to write scripts for the new script function.

Results

You've created a script object. You can use it in any widget event handler.

You can add multiple script functions to the script object by selecting Start of the navigation path Next navigation step Add Script FunctionEnd of the navigation path next to it in the Outline panel.

Example

The following example displays the elements of the script function computeAverage.
  • name: computeAverage

  • return type: number

  • arguments: value1, value2 and value3 (all number type)

  • scripts: return (value1 + value2 + value3) / 3.0;

Modify And Delete Script Objects and Script Functions

To change the name, description, arguments and return type of a script function, select it in the Scripting area of the Outline panel. The panel opens where you can directly update your changes. After you've changed the elements, select Done to close the panel.

To edit an argument, in the Arguments section of the Script Function panel, select (Edit) when hovering over it. To remove an argument, select (Remove) next to it.

To delete a script object or script function, select next to it and then Delete.