Script Objects versus Script Actions

When should you use a Script Object versus a Script action?

Script Objects are mainly intended to hold self-contained functions which are then called by Script Actions. It is not recommended practice to write script actions into a Script Object as you cannot control properly when the script will be run. In particular you should note that script in Script Objects will not be run in Flash.

Consider the following

Let’s use an example to explain this concept - imagine a publication that provides a Currency Conversion Calculator that calculates the conversion rate for six different countries. The following solution will show one way of creating the Currency Conversion Calculator that combines the two methods of adding an OpusScript to a publication.

Example Currency Conversion Calculator

  1. Create one Script Object for the page. Write the six functions that convert the amount entered by the user to the local currency e.g. function FrenchConverion(); function ItalianConversion() etc.

  2. Create seven buttons on the page and add a Script action to each - use Left Mouse Click as the Trigger for the Script Action.

  3. For the first six buttons call one of the functions you have declared in the Script Object e.g. for the first button call the French conversion function i.e. FrenchConversion(); for the second button call ItalianConversion().

  4. For the seventh button, call all six functions.

In this example, the Script Object holds all the calculations and the Script actions simply call the same Script Object. The Currency Conversion Calculator could have been created using only Script actions; the conversion calculation for each country could have been written in Script actions on the buttons. However, the seventh button would have had to repeat the code on the other buttons.

Tip:

Add one Script Object to a page. In the Script Object, add all the calculations and functions required for that page. Call the required functions in the Script Object using Script actions.

Advanced Tip:

Functions in a Script Object are only available to other objects on the current page and not to other pages or other objects on other pages within your publication. If the same functions are required on multiple pages within your publication, create one External Script that contains all the functions for your publication and import the External Script as a Script Object to every page that requires it – see External Script for more information.

Related Topics:

Add a Script Object to a Page

Add a Script Action to an Object