OpusScript Syntax

The syntax of a language is how the various elements of the language are combined to make a statement. OpusScript uses some of the basic elements and syntax of JavaScript and then allows you access to all the objects of an Opus publication via their object names.

Functions or properties of those objects are referenced by using a full stop. Thus the script action to Show an Object called myImage is:

myImage.Show()

All objects are sub-objects (called child objects) of another, for example the individual frames of a MultiFrame are children of the parent MultiFrame object. You can access these by using their object names just as though they were independent objects. You do not need to include the name of the parent object but if you do not know the name of a particular object then you can use the GetParent function to refer to it. If you want the script to refer to the object it is located on you can also use the keyword this in place of the object name and the synax then becomes:

this.Show()

A list of the programming elements is provided at the bottom of this topic.

For a list of all the functions available go to OpusScript Reference.

To start writing an OpusScript go to Where do I write my OpusScript?

Single Statements

Simple statements are written on single lines and can be as simple as applying an Opus function to an Opus object. The object is referred to by the name you have given it in the Organiser. This is then followed by a full stop and then a function you want the object to perform with any parameters required in brackets thereafter.

myImage.Show()

The above will cause an Image object on your page called myImage to be shown, if it is hidden. This is the script equivalent to applying a Show action to an object. The brackets indicate that the Show function can take a parameter (extra details to specify its activity) – in this case you can specify an exact position for the object to be shown. The parameters and syntax for each individual function are provided in this help file.

Some of the functions of an Opus object return information and so they have to be used inconjunction with a variable to store that information in. Thus,

myImage.GetPosition()

won’t do anything. Instead you must use another variable to store the position in thus,

var currentPosition=0

currentPosition=myImage.GetPosition()

In some cases the property which is returned by a function can have sub-properties of its own which become sub-properties of the variable you assigned the property to. Thus, GetPosition() yields two co-ordinates x and y and these can be addressed individually by using the sames syntax – the variable name then a period (fullstop) followed by the property you require. The script below gets the current position of an object called myImage and then uses the x and y co-ordinates to set another object called altImage in the same position.

var currentPosition=0

var currentX=0

var currentY=0

currentPosition=myImage.GetPosition()

currentX=currentPosition.x

currentY=currentPosition.y

altImage.SetPosition(currentX,currentY)

 

Simple statements can also be used for assigning values or calculating expressions by combining data in variables using one or more of the operators.

Tax = Amount*TaxRate

In this case we are making one variable equal to a second variable multiplied by the third variable. Each of these pieces of information are variables containing data of the same type (numbers).

If we were putting this into a function we would need to "declare" the variables first. This means we would list them as being variables

var Tax
var Amount
var TaxRate

We then might want to "initialise" the variables to give them a value before they were used.

TaxRate = 17.5

Or we can do both at the same time

var TaxRate = 17.5

If you do not use var to declare variables, the variable is seen outside the immediate function and would keep its value after the function was finished. This might cause confusion and a bug if you were to inadvertently use the same name again and the variable came to that point already containing a value that you were not expecting. This is also one reason why programmers will often initialize a variable before using it so they can be confident of its content before moving on to use it.

Note:
OpusScript has three types of "equals". Using one equals sign ( = ) is using equals as an assignment operator. That is the item on the left of the sign is given or assigned the same value as the item or expression on the right hand side.

Using two equals signs together ( == ) is using equals as a comparative operator, which means it checks to see if the items on both sides of the expression are the same.

Using three equals signs (===) is a strict version of the comparative operator which will check to see if the items on both sides of the operator are not only equal in terms of content but also type. Thus it will not find the number 1 and the text representation of "1" as the same, whereas the basic comparative operator will.

Parameters

If you want to write an expression as a parameter (or setting) for something it should be written inside brackets after the item.

Pathnames

If you wish to include backslash characters in literal strings in OpusScript, for example in pathnames such as:

c:\windows\temp\myfile.rtf

then, in common with the ECMA standard, you need to use double backslash characters, i.e.

c:\\windows\\temp\\myfile.rtf

Series of Statements, Loops and Conditional Actions

If you want to write a series of statements they should be placed on separate lines between two curly brackets.

if (totalScore >= 70)
{
passImage.Show()
}
else
{
failImage.Show()
}

Here we are checking to see if a user’s total score is better than 70 in which case we show the "Pass" image (a publication object called passImage). Otherwise we show the "Fail" image (a publication object called failImage).

Note:
In common with most programming and scripting languages, and in order to maintain compatibility with the ECMA-262 Standard, OpusScript is case sensitive. This means that ‘calculateTax’ is not the same as ‘CalculateTAX’.

Although, this allows you to have, for example, a function named ‘CalculateTax’ and a variable named ‘CalculateTAX’, we strongly advise that you do not rely on capitalisation to distinguish between functions and variables.

Shortcut for Incrementing a Value

If you want to increment a variable you can use a double plus sign.

loopcounter++

is shorthand for

loopcounter = loopcounter + 1

This inverse also applies to the decrement value:

loopcounter--

is shorthand for

loopcounter = loopcounter - 1

OpusScript Elements

The following elements are provided within OpusScript Elements

Data Types:

Number, String and Boolean

Objects:

Date, Math and Array objects,

plus Opus Publication objects which are referred to by their object name as seen in the publication.

Object

Operators:

Arithmetic, String, Assignment and Logical Operators

Loop Statements:

for and while

Conditional Statements:

if and else

Other Statements:

function,

eval, break,

fork,

return, var

wait