General Information
about Script-Based Control in SpectraLab
Basics
Besides traditional "pick and
click" control via GUI (Graphical User Interface), SpectraLab
may be controlled through textual commands utilizing its
scripting language. Script-based control provides powerful means
for automated data processing. It also allows the user to define
new "words" (commands) that can be added to the SpectraLab
vocabulary permanently (i.e., saved between the sessions), thus
providing the user an opportunity to tailor the software for
various specific applications.
In order to allow versatile
script-based control of the program, the user interface of
SpectraLab was written in Forth (Win32Forth), an
extensible interactive stack-based language, which allows the
user to write and execute custom pieces of code "on the
fly". The scripting language can be used in two ways:
- For immediate execution of the
scripts, they can be entered as a sequence of commands into
the Command Line edit box. It is found below the main toolbar
in the Splab-Controls pane. Pressing the <Enter> key on
the keyboard or clicking on the "Exec" button next to the
command line box will cause the execution of the entered
commands.
- The user can also add new
words to the SpectraLab dictionary by placing the scripts into
the STARTUP.F file, automatically loaded upon starting
SpectraLab (if present in the program's main folder). These
new scripts become an integral part of SpectraLab vocabulary
and can be executed by typing their names in the Command Line.
The commands (scripts) should be
written according to the general rules of the Forth programming
with the use of either basic words of Win32Forth or
SpecrtraLab-specific words described in "Basic Glossary of
SpectraLab" and "Screen Output of Textual and Numeric Data in
SpectraLab" pages.
The use of Command Line
Although the Command-Line scripts
should follow the general rules described in the following pages
of this Help, there are several limitations and exceptions from
the Forth standards for the scripts entered for immediate
execution::
- These scripts should not
contain any flow-control constructs, such as
"IF...THEN..ELSE", "DO..LOOP", "CASE...ENDCASE", etc.
- To repeat the execution of a
sequence of commands multiple times (i.e., organize a cycle),
the user can start the Command Line with the construct "do(n)",
where n is the number of desired repeats. The n
parameter in this construct should be an explicit integer
number. Arithmetic operations or the names of variables inside
parentheses are not allowed. The construct "do(n)" is
Command-Line-specific - it can appear only at the beginning of
the Command Line and cannot be used in the scripts defined in
STARTUP.F.
- Command Line provides the user
an option to enter the floating-point (real) numbers in
conventional form with a decimal point (like 3.1415, 0., 0.0,
etc.), in contrast to the basic Win32Forth, which requires
preceding these numbers with "F#" word or entering them in
scientific notation (like 6.0221408E23 or 0E0). The
conventional form of the real numbers cannot be used in the
scripts defined in STARTUP.F - they should strictly follow
Win32Forth syntax.
Adding new words to the SpectraLab dictionary and the use of the
STARTUP.F file
In order to use the same script
multiple times, the user can define it as a new word in the
SpectraLab dictionary. It can be done either in the Command Line
or by including its definitions in the STARTUP.F file. While the
new words defined through the Command Line can be used only in the
ongoing session and are lost upon closing the program, the
definitions found in STARTUP.F become an integral part of
SpectraLab and are preserved between the sessions. According to
the general rules of Forth, the definitions should start with a
double-point (":") followed by a space and a name of the
new script (which will be used to invoke its execution from the
command line). The definition should end with a semicolon (";")
preceded by a space (or form a separate line in STARTUP.F). The
names of new definitions (new words) can contain any letters,
numbers, and special symbols (no spaces, however) and can be of
any length. They are not case-sensitive. In order to prevent
re-defining already existing words, the user is recommended to
start the new names with such special characters as a percent sign
(%), tilde (~), or underline (_). When defining new words, either
in the Command Line or in the STARTUP.F file, the user should
strictly follow the syntax of Forth - the limitations and
exceptions described above for the immediately-executable scripts
are not applicable. Some examples of valid definitions of new
SpectraLab words may be found in the STARTUP.F file supplied with
the package. It should be noted, however, that any errors in the
STARTUP.F file (misspelled words, missing ";" at the end of
definitions, etc.) will prevent SpectraLab from loading with the
appearance of "Exception occurred in program" error notice.
Debugging of complex scripts can be done with the use of installed
Win32Forth package and SpectraLab source files available from the
author upon request.