Analytica Script

Revision as of 21:59, 17 October 2017 by Max (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


Analytica Script is a variant of the standard Analytica modeling language with a slightly different and more limited syntax. It is now rare that Analytica users need to learn about Script, since most of its commands are now have equivalent Functions in the standard Analytica language -- for example, CreateObject, ShowWindow, CloseWindow. The main use of Analytica Script is in the Typescript window, an interpreted text-based user-interface to Analytica. The Typescript is a relic of predecessor to Analytica, now used only occasionally by Analytica experts -- usually as an aid for debugging. Analytica Script is required in the Script attribute of a Button or Picture, but this attribute is now obsolete, having been replaced by the OnClick, and OnChange attributes, which use standard Analytica syntax. The Script attribute is retained only for legacy models. This page (and its links) are for those intrepid few that still want to learn about Script.

You can define Scripts in:

  • The Script Attribute of a Button, Picture, or input varibale. The Script is executed when the end user clicks the button, or changes the definition of an Input variable. The Script attribute is deprecated, having been replaced in Analytica 4.6 by OnClick, and OnChange attributes, which use standard Analytica syntax, and also allow assignment to Global variables.
  • The Typescript Window: Press Ctrl+' (control+apostrophe) to open this window. It offers a command line interface to Analytica.
  • In the special function EvaluateScript(script), which may be used in normal Analytica language. It evaluates its text parameter, script, as a script, and so lets you execute a script anywhere -- very powerful and a bit dangerous.

Button Scripts

You create a button in a Diagram like any other object type:

  1. In Edit mode, drag the button icon from the far right of node type toolbar to the position on the diagram you want (or press control-0).
  2. Double-click the button to open its Object Window.

Now you can review and edit its Script attribute.

Choice and Checkbox variables

You can add a Script that gets executed when the user selects a new Choice or (un)checks a Checkbox for a user-input variable defined as Choice(i, n) or as Checkbox(bool):

If Script attribute does not appear in Object view of the variable, you set it to appear:

  1. In Edit mode, select Attributes... from the Object menu.
  2. In the list of attributes, scroll down until you find Script. Check it, and click OK.

You can then type into the Script attribute of a selected variable.

Script syntax

Each line in a Script may be a standard expression from the Analytica language, such as a call to a function. There are some minor differences in syntax: You can put expressions or commands on separate lines, without needing a semicolon ";" between the lines

Assigning to globals

Within a Script, you can assign a new Definition or other Attribute to a global Variable, e.g.

Y := X + 100
Definition OF Y --> X + 100

When you execute the script (e.g. click the button), it assigns X + 100 as the new Definition of Y.

If you enclose the assignment in parentheses, it assigns the value, rather than the expression, as the new Definition:

(Y := X + 100)
Definition OF Y --> 110

Analytica does not usually let you assign a new value to the Definition (or other Attribute) of a Global variable (or other Object). This is to maintain the no side-effects rule, which makes Analytica models so much easier to understand and debug than normal programs. Computer scientists would say it is a functionalrather than procedurallanguage.
But, sometimes you do want to write a procedure that makes assignments to Global (i.e. nonlocal) variables. You may also include assignments to Globals in a Function that is called from a Button script -- or from a Function that is called from a Function called from a Button script, etc. In this case, the end user is explicitly pressing the button and initiating the change to the model -- and so, side-effects are allowed.

Script Commands

A third key difference of Scripts is that the availability of a variety of Script commands -- e.g.

Open Object Y

opens the Object window of Y. Script commands take a list of parameters, usually object names, without parentheses around the parameters and without separating commas.

These Script commands are mostly a relic of Demos, an early predecessor of Analytica. Some no longer work or are otherwise deprecated (discouraged), but a few can be useful for special purposes. For more, see Commands and Scripting Guide.

See Also

Comments


You are not allowed to post comments.