snap_manager scripting

The snap manager program uses a scripting language to define menu options. This can be customised as required. When the program starts it loads a script called "snap_manager.cfg" located in the same directory as the snap_manager program. This sets up the menu options, then looks for additional configuration scripts in the scripts subdirectory of the program. It will run any scripts called "*_menu.cfg", and then a script "local.cfg" if it exists. Local customisations are best installed into the local.cfg file. All the menu actions are implemented by functions. These functions can be overwritten in the local configuration to change their behaviour.

Scripting reference index

Constants
Variables
Function calls
Expressions
Statements
User defined functions
Menu items
Dialog boxes
System variables
Built-in functions
Regular expression syntax


Constants

Constants are either string or logical values (true or false). A non-empty string is equivalent to the logical value true.

String constants are enclosed in quote marks, eg "string". The may span several lines in a script. They can include "\t" for tab characters, "\n" for new line, and "\r" for carriage return. Use "\\" to include the backslash character in the string.

Strings may also include variable names ($name), which will be substituted with the value of the variable. The name may be terminated with an additional $ character if necessary to avoid ambiguity. Use "\$" to include the dollar character in the string.

Strings can also be quoted with single quotes ('). In this case the text between the quotes will be returned without any modification. This text can include any characters except single quotes.

Finally, strings can be entered as unquoted integer values (one or more digits).


Variables

Variables are named with a leading "$" character followed by letters, numbers, or underscore characters. For example "$my_job_file". Some variable have predefined values .. see System variables below. Variables starting with "$_" are global variables ... they remain defined until snap_manager terminates. Other variables exist only for the duration of the script or function in which they are defined. For example:

$errorfile = FindJobFile(".err")


Function calls

Functions are called by a function name followed by zero or more parameters in brackets. snap_manager defines a number of built-in functions. The script can also define new user defined functions. Examples of function calls are:

SetConfig("editor","notepad.exe")

$tempfile = TempFile()


Expressions

Expressions define values calculated from constants, variables, and other expressions. Expressions can include the following operators

.

String concatenation uses the "." operator. For example

$name . $extension

+, -, *, /

Numerical operator - variables are interpreted as numbers and added, subtracted, multiplied or divided

not

Reverses the logical value of an expression. For example

not FileExists($datafile)

and, or

Logical operators combining variables

==, !=

String comparison operators. Return true when strings are equal (==), or not equal (!=).

<, <=, >=, >

Inequality operators, return true if the left operand is less than, less than or equal to, greater than or equal to, or greater than the right operand. Comparison of strings is slightly non-intuitive. The strings are interpreted as floating point numbers up to the first non-valid character, then as a string residual. Comparison is first on the numerical value of the numbers, and if they are the same, then on a text based comparison of the residuals. If the beginning of the string cannot be interpreted as a number then the number part is taken to be 0.0, and the residual the entire string.

Expression can be combined with parentheses to define the order of evaluation, for example

not ($job_valid and FileExists($binfile) )


Statements

The basic statements of the scripting language are assignment statements, conditional statements, and loop statements.

Assignment statements assign a value to a variable, using the "=" operator. For example:

variable = expression

Assignments can also assign multiple values at once, as in

variable1,variable2,.. = expression1,expression2,..

Conditional statements define statements that may or may not be executed, depending upon some conditions. These are structured as

if expression then statements endif

The statement may also define alternative branches using elseif and else components. For example:

if FileExists($errorfile) then
   if Ask("There were errors in the job\r\nDo you want to see the error file?","Errors") then
      Start($_editor,$errorfile)
   endif
elseif not FileExists($resultfile) then
   Message("Something went wrong!")
else
   Message("The job completed successfully")
endif

There are two forms of loop statement. The "while" statement is

while expression do statements endwhile

This will execute the statements repeatedly as long as the expression is true. By default this will iterate a maximum of 100 times if the expression remains true. The maximum number of iterations can be reset by defining a value for the variable $max_while_loop_iterations, for example:

$max_while_loop_iterations="3"

The foreach statement has several formats. It can either be specified with a set of expressions, as in

foreach variable in expression1, expression2, ... do statements endforeach

In this case the variable will be set to each expression value in turn and the statements in the loop are executed.

If just one expression is specified then it is interpreted as a string and the loop is executed setting the variable to each line in the string (delimited by "\n").

foreach variable in expression do statements endforeach

Each form can be used with a "delimited_by" clause, in which case the string (or strings if there is more than one expression) will be split using the regular expression defined in the delimited_by clause. If the expression contains capture groups then the variable is set to each of these in turn as well as the delimited text. The format of this is

foreach variable in expression delimited_by delimiter_expression do statements endforeach

The final form of the foreach statement finds all substrings within an expression that matches a regular expression

foreach variable in expression matches match_expression do statements endforeach

This will return every occurrence of the the match expression (again a regular expression) within the string. If the expression contains capture groups then only the capture groups will be returned, otherwise the matched strings will be returned.

There are also four exit statements available

continue

break

return expression1, expression2, ...

exit

The continue statement applies within a while or foreach loop, and causes the rest of the statements in the loop to be skipped, immediately starting the next iteration of the loop. The break statement terminates the current while or foreach loop. The return statement exits the current function and specifies the value or values to return. The exit statement exits the current script.


User defined functions

User defined functions may be defined with the syntax:

function name( $param1,$param2, ... )
   statements
end_function

This is activated by a function call defined which defines the values of each of the parameters. Any missing parameters are defined to be empty strings.

The function can include one or more return statements defining a value to return to the calling script. Otherwise the function returns the value from the last statement executed. The return statement can also multiple values, for example

function minmax( $v1,$v2 )
   if $v1 < $v2 then return $v1, $v2 endif
   return $v2, $v1
end_function

$vmin,$vmax=minmax(5,3)