2d. Characteristics of the QSA Language: Getting started with QSA

Declare & initialize variables

Conditional expressions

Looping

Create function with arguments

Return a value from a function

Receive user input

Exceptions

 

 

Declare & initialize variables

In the QSA language, variables can be simultaneously declared and initialized: 'var x = 20;'. All variables are declared with 'var'. The actual type of the variable is checked during execution of the script.

Conditional expressions

In order to check whether a condition is true, conditional expressions are used. A comparison condition in the QSA language should be written between ( ). In the QSA language a difference is made between the assignment operator '=' and the comparison operator '=='; in contrary to Visual Basic Script, where only '=' is used. In QSA it is even possible to distinguish between value and type by using the '===' operator.
The same is valid for negations:

A short way to execute statements dependent of whether a condition is true or not is 'expression ? resultIfTrue : resultIfFalse;'. Example: '(docFMR.indexOf("untitled") > 0) ? docFMR.SaveAs("CG_OBJECTS.fmr") : docFMR.Save(); '.

Looping

The QSA language offers different ways to increment counters in order to loop until a condition is satisfied. Incrementing can be done before or after the value is returned: 'x++' or '++x'.

Create function with arguments

When a parameter is passed to a function, it is not necessary to include the datatype. The fact that there is a parameter passed, can be indicated in between the ( ): function getPath( intPathnumber ) {. When there are more than one parameters, separate them by comma's.

 

Return a value from a function

For returning values from functions, a value name without type can be used. All variables can be declared as var. In the function showReturnVal() below, the function returnVal( <variable 1>, <variable2>) is invoked.

 

The function returnVal( <variable 1>, <variable2>) (see below) receives in this case the values 600 and 20.

 

 

 

The resulting value '12000' is returned to the showReturnVal() function that shows the values via a message box:

 

 

 

 Receive user input

 

User input can be received directly from the QSA input classes. For example this statement var fmrName = Input.getText("Please enter the name of the new FMR project"); results in this input dialog:
 

.
 

The user input is stored in the string variable 'fmrName'.
It is also possible to retrieve the values from graphical components on self-made dialogs.
 

Exceptions

An error that occurs while the script is processed might cause damage to created files. Therefore it should be nice to know whether an error occurs and the better would even be to prevent possible damage.
The QSA language provides the 'try... catch' and 'try... finally' functions to catch an error immediately after it occurred. For important processing errors can be caught in the following way:

 

try {

bvDoc = BrainVoyagerQX.CreateProjectFMR( ...parameters... );

} catch (e) {

MessageBox.warning("Did not succeed in creating a FMR project! Error message was: " + e);

}

 

The 'try... finally' method is used when a certain statement always should be processed. Files can be saved in the finally block, for example.
The 'throw' method is applied for user defined errors: 'if (fmrDoc.TR == 50000) { throw "TR too high!" }.

 

Next: 2e. Script Editor preferences
Index