Writing scripts
This part of the guide is for the script author: the person who writes the scripts that an application runs. It describes the script languages, the routines available to every script, and how scripts declare forms and classes and use other scripts. Applications that let their users write scripts can give these chapters to them.
TMS Scripter supports three script languages. Each one has a complete reference chapter, with the same structure:
- Pascal language reference: the syntax of Delphi (Object Pascal).
- Basic language reference: a syntax similar to Visual Basic and VBScript.
- JavaScript language reference: the syntax of JavaScript.
The other chapters apply to all languages:
- Built-in routines: the routines available to every script.
- Calling DLL functions: declaring and calling functions of DLLs.
The application decides which languages its users can write scripts in. In the TMS Scripter
IDE, the language of a script unit is shown by its file extension: .psc for Pascal, .bsc for
Basic and .jsc for JavaScript.
What all languages have in common
The three languages are different syntaxes for the same scripting engine, so they share these characteristics:
- Names are not case-sensitive, in all languages: keywords, variables, routines, and the objects and members of the application.
- Values have no fixed type. A variable can hold a number, a string, a Boolean value, a date, an object or an array, and the values are converted automatically when needed, following the same rules in all languages.
- The objects of the application (forms, controls, data, business objects) are used with their properties and methods. The application decides which objects, classes and libraries are available.
- The same built-in routines (
ShowMessage,IntToStr,Copy,FormatDateTimeand many others) are available in all languages. - Scripts of different languages work together. A script can use a script written in another language as a library, and call its routines, use its variables, and create its forms and classes.
Using other scripts
A script can use another script as a library: it can call the procedures and functions of the
other script, and read and write its global variables. The uses clause (Pascal and Basic) or
the import statement (JavaScript) names the scripts and libraries a script uses.
The application defines where the used scripts come from: other scripts it manages, or script files in a folder. The main block of a used script does not run when another script uses it.
See the section "Using libraries and other scripts" of each language reference: Pascal, Basic, JavaScript.
Declaring forms in script
A script can declare a form: a form file (.dfm) gives the design of the form, and the script
gives its behavior, with its variables, routines and event handlers. Other scripts create
instances of the form and use it much like a form of a Delphi application. A directive at the
beginning of the script gives the form class name and the form file: {$FORM TMyForm, myform.dfm} in Pascal, #FORM TMyForm, myform.dfm in Basic and //$FORM TMyForm, myform.dfm
in JavaScript.
See the section "Declaring forms in script" of each language reference: Pascal, Basic, JavaScript.
Declaring classes in script (script-based classes)
A script can declare a class: its global variables are the properties of the class, its
routines are the methods, and its main block initializes each new instance. Other scripts
create instances of the class and use them like other objects. A directive at the beginning of
the script gives the class name: {$CLASS TMyClass} in Pascal, #CLASS TMyClass in Basic and
//$CLASS TMyClass in JavaScript. Each class is declared in its own script.
See the section "Declaring classes in script" of each language reference: Pascal, Basic, JavaScript.
Notes for application developers
This section is for the developers of the application that runs the scripts.
Languages. TatScripter runs scripts of all languages. The language of each script is chosen when the script is created; see Cross-language feature: TatScripter and TIDEScripter.
Used scripts. The names in a uses clause or import statement are resolved against the
registered libraries, the scripts of the scripter, and script files; see
Loading script libraries from files.
The DFM files of form scripts are searched the same way, using the search path of
TatCustomScripter.LibOptions.
Form files. The DFM file of a form script must be a Delphi DFM file in text format, without event handlers. All the component classes used in the DFM must be registered before the form is created, otherwise a "class not registered" error is raised:
RegisterClasses([TEdit, TButton, TListBox]);
Instead of loading a DFM file, set the TatScript.DesignFormResource property
of the form script to the form resource in binary format: when it is not empty, the file named
in the FORM directive is not used.
Script-based classes. Classes declared in script are not Delphi classes: all their
instances are objects of the TScriptBaseObject class, or of the class set in
TatCustomScripter.ScriptBaseObjectClass, which must descend from
TScriptBaseObject. That is why script classes cannot inherit from other classes. Instances
that the script does not free are destroyed by the scripter component.