CRT Icon  Creating Scripts


 

CRT is capable of hosting "ActiveX Script" engines. The most common ActiveX script engines are VBScript and JScript (MicrosoftÆs version of JavaScript), both of which are freely available from Microsoft. Chances are you already have them installed if youÆve installed Internet Explorer 4.0.

 

ActiveX script engines communicate with CRT via standard interfaces. Therefore, CRT can host any compliant script engine to run your scripts. The advantage of this approach is that you can script CRT using the language of your choice. If an ActiveX script engine is available for your preferred scripting language, you can write scripts that will work with CRT

 

Scripts for CRT are simply text files that you create with your text editor. Each script that CRT runs must have a header that begins on the first line of the script. The header is used by CRT to identify which script language the script is written in and the version of CRT's scripting interface. Each line of the script header must begin with a (#) character. The CRT script header requires a $language line which identifies the script engine and an $interface line to identify CRT's interface version.

 

The syntax of the script header is always the same regardless of the script language you are using.

 

A simple but complete CRT script with a header that identifies it as VBScript is shown below:

 

# $language = "VBScript"

# $interface = "1.0"

 

Sub Main

  ' Display CRT's version

  MsgBox "CRT version is: " & crt.Version

End Sub

 

Note, a CRT script header may also contain blank lines that begin with (#).

 

The quoted string following $language identifies the script engine. If you are writing scripts that use MicrosoftÆs JScript language, the appropriate identifier is "JScript". If you are using another script engine youÆll need to consult the documentation for the identifier for that language. Currently the script header should specify version "1.0" for $interface. Future versions of CRT may support other versions. The example script above has a subroutine named "main" where all of the scriptÆs code is located. When CRT executes scripts it always attempts to run a "main" routine if you have defined one.

 

It is not a requirement that you place your code within a "main" however there may be reasons why you would want to do this. The VBScript and JScript engines will parse and execute global script code (script code you have defined outside of any subroutine) before your "main" is executed. If you have "initialization" code that you want to ensure has been completely executed before your actual script code begins, it may be useful to place your initialization code at the global level. This will ensure that your initialization code will all execute before your "main" code runs.

 

Another reason you may want a "main" routine is to allow your scripts a way of aborting themselves in case of problems. In VBScript there is no built-in way of exiting a script at the global level. However, if you want to exit a subroutine it is possible to use the "Exit Sub" syntax to do so. For example, in VBScript:

 

Sub Main

 

  condition = DoSomething()

  If condition = 0 Then

    ' Error, bailout

    Exit Sub

  End If

   

End Sub

 

When the "main" routine ends the script has finished running. By placing your code within a "main" you have the option of invoking "Exit Sub" whenever it might be necessary.

 

The previous script samples are written in VBScript. The remainder of code samples in this document are all written in VBScript unless it is stated otherwise. The properties and methods of CRT's interface can be used as documented by any compatible scripting language.

 

See the sample scripts for examples of CRT scripts using languages other than VBScript.

 

See also:

 

Handling Script Errors

Overview of Script Objects