snap_manager scripting system functions and variables

The snap manager program uses a scripting language to define menu options. This can be customised as required. This page describes the built in system variables and functions that can be used in scripts. When the program

Scripting reference index

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


Function index

AppendFile(filename,text)
Ask(message,title)
BrowseFile(message,filename/url)
ClearLog()
CopyFile(from_filename,to_filename)
DeleteFile(filename)
DeleteMenu(menuname)
Directory(path, filespec)
DirectoryExists(dirname)
ExecuteFunction(functionname,parameter,...)
FileExists(filename)
Filename(filename,part,path)
FindConfigFile(section,name,extension)
FindJobFile(extension)
FindProgram(programname)
GetConfig(item)
GetDate(format)
GetEnv(envvar)
GetOpenFilename(prompt,filter,multiple)
GetSaveFilename(prompt,filter)
GetValue(variablename)
InsertPath(path,envvar)
LoadJob(jobfile)
Log(message)
LogRun(program,parameter,...)
LowerCase( string )
MakeDir(dirname)
Match(string,regex,nMatch)
MatchGroups(string,regex)
Message(message,title)
ReadFile(filename,regex,count)
RemoveDir(dirname)
RenameFile(from_filename,to_filename)
Replace(string,regex,replacement,max_replacements)
Run(program,parameter,...)
RunScript(scriptfile)
SetConfig(item,value)
SetEnv(envvar,envvalue)
SetExtension(filename,extension)
Start(program,parameter,...)
SubString( string, start, length )
TempFile()
UnloadJob()
UpdateJob()
UpperCase( string )
WriteFile(filename,text)


System variables

The following system variables are defined by snap_manager.

$job_valid

A job file is loaded into snap_manager and appears to be valid.

$job_file

The name of the current job file.

$coordinate_file

The coordinate file of the current job

$data_files

The list of data files defined for the job, separated by newlines.

$job_title

The title of the current job

$job_path

The base directory of the current job. (Note that when a job is loaded the base directory is made the current directory).

$load_errors

A list of errors encountered loading the job file - currently this is almost empty as the snap_manager program does almost no testing of the job files.

$snap_path

The snap program directory. Note that this is included in the PATH environment variable for any programs run by snap manager.

$system_config_path

The base directory for system SNAP configuration files.

$user_config_path

The base directory for user SNAP configuration files.

$system_script_path

The system snap manager script directory.

$user_id

The system user id of the user running snap_manager.

$user_script_path

The user snap manager script directory.

$script_file

The filename of the snap_manager script file in which the variable is used.

$coordsys_list

A list of coordinate system names and codes for use in a dialog box selector.

$heightref_list

A list of coordinate system names and codes for use in a dialog box selector.

$coordsys_file

The name of the coordinate system definition file.

$is_windows

True if SNAP is running on a windows platform.

$is_linux

True if SNAP is running on a linux platform.

$version

The version of snap/snap_manager being run.

$version_date

The date on which this version of snap/snap_manager was built.


Built-in functions

The following system functions are defined by snap_manager.

Message(message,title)

Displays a message box with the specified message and title. Always returns true.

Ask(message,title)

Displays a message box with the specified message and title, and with Yes, No, and Cancel buttons. Returns true or false if the yes or no buttons are pressed, and aborts the script if Cancel is pressed.

GetOpenFilename(prompt,filter,multiple)

Request the name of an existing file from the user. The prompt is used to title the dialog box, and the filter defines the files shown. The filter is formatted as "List files (*.lst)|*.lst|All files (*.*)|*.*". This will only return the name of an existing file. The multiple parameter is optional .. if true then the user can select more than one file.

GetSaveFilename(prompt,filter)

Request the name of file to save information to from the user. The prompt is used to title the dialog box, and the filter defines the files shown. The filter is formatted as "List files (*.lst)|*.lst|All files (*.*)|*.*". This will prompt to warn the user of overwriting if an existing file name is selected.

FindProgram(programname)

Looks for a program on the executable path, and returns its full path if found. Returns false if it is not found. For example: FindProgram("perl.exe").

LoadJob(jobfile)

Loads the specified job into snap_manager. Returns true if the job is successfully loaded.

UnloadJob()

Unloads the job from snap_manager. Returns true if the job is successfully unloaded.

UpdateJob()

Causes snap_manager to reload the information from the job file, such as the job_title, etc. This will normally happen when a script function completes in any case .. this function is only required if updated information is required before a script completes.

FindJobFile(extension)

Looks for and returns the name of a file with the same name as the job file, and with the specified extension. Returns false if the file does not exist, or the filename if it does. For example: FindJobFile("err")

Directory(path, filespec)

Returns a list of files in the specified directory separated by newline characters. If filespec is given then only the names matching that are returned.

Filename(filename,part,path)

Returns the specified part of a filename. Options for part are "path", "name", "extension", "fullname" (name + extension). Also "absolute" to make the name absolute in terms of the specified path (default current directory), and "relative" to make the name relative to the specified path. Note that when a job is opened the command file directory becomes the current directory.

SetExtension(filename,extension)

Returns a filename based on a source file name but with a specified extension.

FileExists(filename)

Returns true if the file exists, false otherwise.

DirectoryExists(dirname)

Returns true if the directory exists, false otherwise.

MakeDir(dirname)

Creates the specified directory if it does not already exist. Also makes any required parent directories. Returns true if the directory exists or is successfully created, false otherwise.

RemoveDir(dirname)

Removes the specified directory. The directory and any subdirectories or files it contains are removed. Returns true if the directory is removed, false otherwise.

DeleteFile(filename)

Deletes the specified file, returning true if the file is successfully deleted (or didn't exist in the first place), and false otherwise.

RenameFile(from_filename,to_filename)

Renames a file. If a file with the target name already exists then it will be replaced with the renamed file. Returns true if the file is successfully renamed, false otherwise.

CopyFile(from_filename,to_filename)

Copies a file. If a file with the target name already exists then it will be replaced with the copied file. Returns true if the file is successfully renamed, false otherwise.

TempFile()

Creates and returns the name of a temporary file.

ReadFile(filename,regex,count)

Reads the contents of the file into a variable. If regex is supplied then only lines matching the expression are returned. If count is supplied and is greater than zero then no more than this number of lines is returned.

BrowseFile(filename/url)

Opens the file or url in the default web browser.

WriteFile(filename,text)

Writes the specified text to the file. If the file already exists then it will be overwritten.

AppendFile(filename,text)

Appends the specified text to the file. If the file does not exist then it will be created.

RunScript(scriptfile)

Runs the script in the named file. The script should be in the scripts subdirectory of the SNAP program directory.

Run(program,parameter,...)

Runs a program with the specified parameters. Waits for the program to finish. If only the program name is defined and it starts with a quote mark ("e;), then it is assumed to be a quoted command line, rather than the name of a program. For example:

Run("\"$_perlexe\" -q $options \"progdir/long program name.pl\"")

LogRun(program,parameter,...)

Similer to the run() function, except that output from the program is collected and returned by the function, rather than being displayed as it occurs.

Start(program,parameter,...)

Starts a program with the specified parameters. Does not wait for the program to finish.

ExecuteFunction(functionname,parameter,...)

Executes the function specified as the first parameter with the subsequent parameters.

GetConfig(item)

Gets the value of a configuration item from the users configuration.

SetConfig(item,value)

Sets the value of a configuration item from the users configuration.

FindConfigFile(section,name,extension)

Finds and returns the name of a configuration file in the configuration section section and named name. extension is an optional parameter which will be used as the file name extension if the file is not found without the extension This will search first in the user configuration, then in the system configuration. Returns an empty string if no such file is found.

LowerCase(string)

Returns the string converted to lower case.

UpperCase(string)

Returns the string converted to lower case.

SubString(string,start,length)

Returns a substring of the input string starting at the start character, where the first character of the string is 0. The optional length parameter defines the number of characters to select. If it is omitted the string from the start character to the end is returned.

Match(string,regex,nMatch)

Returns the first match of the regular expression in the string, or false if there is no match. The nMatch parameter is optional. If present it identifies the number of the match group to return (match groups are defined by parentheses in the regular expression). The default behaviour is to return the entire matched substring.

MatchGroups(string,regex,global)

Returns a value for each match group in the regular expression as it is matched in the string. If the optional global parameter is true, then returns values for each match of the expression in the string, otherwise only returns values from the first match of the expression with the string. For example:

$hour,$mins=MatchGroups($time,'^(\d+)\:(\d+)$'))

Replace(string,regex,replacement,max_replacements)

Returns the input string with each occurrence of the regular expressions replaced with the replacement string. The last parameter is optional, and specifies the maximum number of replacements that will be made (the default value is 10000). Note that the \ and & characters in the replacement string are special characters that reference capture groups in the regular expressions - see regular expression syntax

GetValue(variablename)

Returns the value of a variable from its name. For example GetValue($varname) where $varname contains the name of a variable (eg "$snap_path")

GetEnv(envvar)

Returns the value of a windows environment variable.

SetEnv(envvar,envvalue)

Sets the value of a windows environment variable envvar to the value envvalue, and returns the new value.

InsertPath(path,envvar)

Adds a directory path path to the beginning of an environment variable envvar. The second parameter is optional - by default the variable PATH is updated.

GetDate(format)

Returns the current date and time. The format parameter is optional. If it is supplied then the it is used as a template for the generated date, with the following substitutions being made:

%Y Four digit year
%y Two digit year
%B Full name of month
%b Abbreviated name of month
%m Two digit number of month including leading 0
%#m Number of month excluding leading 0
%d Two digit day of month including leading 0
%#d Day of month excluding leading 0
%H Two digit hour including leading 0
%#H Hour excluding leading 0
%M Two digit minute including leading 0
%#M Minute excluding leading 0
%s Two digit seconds including leading 0
%A Full name of day of week
%a Abbreviated name of day of week

DeleteMenu(menuname)

Deletes the menu or menu item specified. Only items created with menu_item commands can be deleted. The menu name can specify the name of a menu or submenu, in which case all items created in that menu will be deleted. If the name ends with the pipe character "|" then the menu itself will remain and the items within it will be deleted. If the menu name is specified without the pipe character the menu itself is deleted.

Log(message)

Writes the message to the log window.

ClearLog()

Clears all messages from the log window.