RBScript

From Xojo Documentation

Class (inherits from Object)

Used to dynamically execute code within a running (compiled) application.

Events
CompilerError Print
Input RuntimeError


Properties
Context Source State


Methods
Precompile Reset Run

Class Constants

The following class constants can be used to specify the value of the State property.

Class Constant Description
kStateReady The script compiler is ready.
kStateRunning The script compiler is running.
kStateCompleted The script compiler has completed running the script.
kStateAborted The script compiler has aborted.

The following class constants can optionally be passed into Compile(). The default is kOptimizationLevelHigh. Run() does not take a level and defaults to kOptimizationLevelNone unless the script has already been compiled.

Class Constant Description
kOptimizationLevelNone No optimizations will be performed and the script will be compiled lazily (a JIT).
kOptimizationLevelLow The script will be compiled up front, but few optimizations will be applied.
kOptimizationLevelHigh The script will be compiled up front and all possible optimizations should be ran.

Notes

The RBScript language is an implementation of the Xojo language that allows end users to write and execute their own code within a compiled application. Scripts are compiled into machine language, rather then being interpreted.

Since RBScript is a class, you use it by creating an instance of this class either via code or by adding an RBScript control to a window. The easiest way to use RBScript is to assign the code to the Source property of the RBScript object and call the Run method.

To provide information to an RBScript while it's running, use the Input function. This function calls the Input event of the RBScript object where you can return the information you wish returned by the Input function in your RBScript code. In the following example, the results of the Input function are assigned to a variable:

Dim years, days As Integer
years = Val(Input("")) // Prompt the user to enter a value
days = years * 365

The Input function takes a String that can be used to provide a prompt in case you are going to provide a dialog box in which the user enters the information. Since the Input function returns a String and we want to store the value as an integer, the Val function is used to convert the string to an integer. In this case, if the number of years is going to be entered into a TextField called TextField1, then the Input event of the RBScript object would look like this:

Function Input(prompt As String) As String
Return TextField1.Text
End Sub

When the Run method of the RBScript object is called, the code will be compiled and then executed. Since the Input function is called, the Input event of the RBScript object is executed and the contents of the Text property of the TextField is returned and assigned to the Years variable. Then the Days value is calculated.

Output Information

The Print method is used to output data. This method takes a String and passes it to the Print event of the RBScript object. Here is the example modified to use the Print function:

Dim years, days As Integer
years = Val(Input(""))
days = years * 365
Print(Str(days))

You access the value passed to the Print method through the Print event handler. For example, if you want to assign the value to the Text property of a Label object, the code for the Print event of the RBScript object would look like this:

Sub Print(msg As String)
Label1.Text = msg

Handling Errors in Your Code

If an error occurs while RBScript is compiling your code, the CompilerError event of the RBScript object will be called and will be passed appropriate error information so you can then decide how to handle the error. If the error occurs while the code is running, the RuntimeError event of the RBScript object is called and is passed appropriate error information. You can then decide how to respond to the error.

More Information

View these topics for more information about the scripting language, functions and errors:

See Also

Scripting Language, Scripting Functions, Scripting Errors