Class

RBScript

Warning

This item was deprecated.. Please use XojoScript as a replacement.


Description

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

Properties

Name

Type

Read-Only

Shared

Context

Object

Source

String

State

Integer

Methods

Name

Parameters

Returns

Shared

Precompile

optimizationLevel As Integer

Reset

Run

Events

Name

Parameters

Returns

CompilerError

line As Integer, errorNumber As Integer, errorMsg As String

Input

prompt As String

String

Print

msg As String

RuntimeError

line As Integer, error As RuntimeException

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.

Property descriptions


RBScript.Context

Context As Object

The object (e.g., window or class) that is made available to the RBScript.

You cannot pass any objects between the context and the script. This includes, for example, variants and arrays. Any methods or properties that use objects will not be allowed within the script.

For example, you can create a custom class and assign it to the Context property. The script would then have access to the methods and properties of this class. The methods become global methods and the properties become global properties.


RBScript.Source

Source As String

The source code the compiler will run.


RBScript.State

State As Integer

The state of the script compiler.

State can take on the following class constants:

Description

kStateReady

kStateRunning

kStateCompleted

kStateAborted

Method descriptions


RBScript.Precompile

Precompile(optimizationLevel As Integer)

Runs the parser immediately instead of waiting until the next call to Run. Specify the optimizationLevel using the class constants.


RBScript.Reset

Reset

Rewinds the compiler to start over from the beginning.

In this example, script is the RBScript that executes.

script.Reset

RBScript.Run

Run

Runs the code Source until it is done.

Event descriptions


RBScript.CompilerError

CompilerError(line As Integer, errorNumber As Integer, errorMsg As String)

This event gets called if the script could not be compiled due to a syntax error in the code.

line is a one-based line number at which the error may be found. Do not rely on this, though - the error sometimes is found in the succeeding line.

You can look up the meaning of errorNumber in the RBScript.

The errorMsg parameter always returns "Error found while compiling" regardless of the error number.


RBScript.Input

Input(prompt As String) As String

The script requests input from the end user. Returns a String. Used to get input from the user.


RBScript.Print

Print(msg As String)

The compiler is returning the results of the script in msg. Used to display the passed string via a MessageDialog box.


RBScript.RuntimeError

RuntimeError(line As Integer, error As RuntimeException)

A runtime error, error has occurred.

Notes

The RBScript language is an implementation of the Xojo programming 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:

  • Scripting Language

  • Scripting Functions

  • Scripting Errors

Compatibility

All project types on all supported operating systems.

See also

Object parent class;