XojoScript

From Xojo Documentation

Class (inherits from Object)

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

Events
CompilerError Input RuntimeError
CompilerWarning Print
Properties
Context Source State
Methods
Precompile Reset Run
Enumerations
Errors OptimizationLevels Warnings

Notes

The XojoScript 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 XojoScript is a class, you use it by creating an instance of this class either via code or by adding an XojoScript control to a window. The easiest way to use XojoScript is to assign the code to the Source property of the XojoScript object and call the Run method.

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

Var 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 XojoScript object would look like this:

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

When the Run method of the XojoScript object is called, the code will be compiled and then executed. Since the Input function is called, the Input event of the XojoScript 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 XojoScript object. Here is the example modified to use the Print function:

Var years, days As Integer
years = Val(Input(""))
days = years * 365
Print(days.ToString)

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 XojoScript object would look like this:

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

Handling Errors in Your Code

If an error occurs while XojoScript is compiling your code, the CompilerError event is called with 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 XojoScript object is called and is passed appropriate error information. You can then decide how to respond to the error.

MacOS Requirements

Newer versions of macOS may require additional permissions or entitlements in order to use XojoScript in code-signed applications.

See Also