Running Scripts

Top  Previous  Next

Script Execution

 

LMD-ScriptPack provides several ways to execute pieces of script code from the host Delphi application:

 

immediate execution of the global level (not included in procedures) script code;
calling procedures defined in the script;
evaluating script expressions;
executing script statements.

 

These ways are introduced below.

 

Note: The Language property of TLMDScriptControl is to be assigned the appropriate value (corresponding to the script language) before script execution.

 

Two-Stage Script Control Activation

 

Due to the fact (see below) that all global script code is immediately executed during activation of the script control LMD-ScriptPack offers two-stage activation process:

 

1.Prepare
2.Open

 

Prepare method can be used as an intermediate stage to initialize script control, without executing global code. During a call to Prepare method the script control initializes underlaying scripting engine, parses the script code using scripting engine and initializes CodeObject. After a call to prepare method some functionality, inaccessible in the inactive state, become accessible.

 

For example, if you need to create circular references between two script controls to allow to call procedures of the first script controls from the second one and vice versa, you should call Prepare method on both script controls first, then add script controls to each other using AddObject method, and only then open both script controls.  Why this sequence is important? Because only this way the global code of both script controls will be executed after script controls has been added to each other. That is - global code will be able to refer to procedures from another script control.

 

However, of course to make script control fully active, and be able to run script procedures, it is required to call Open method (or set Active property to True) after Prepare.

Calling Prepare method is optional. You can open script control in a single state just calling Open method or assigning True to Active property.

 

Execution of global-level script code

 

To execute some script using TLMDScriptControl you have to specify scripting language setting the Language property. Then assign the script source code to Source property. For example, have a look at following VBScript code:

 

Sub SomeVBProc(str)

  MsgBox str

End Sub

 

SomeVBProc “Hellow World!”

 

Here the first three lines declare VBScript function, and the last line calls it. The last line is denoted as global level code, because it is not embedded in any function declaration.

 

After specifying source code we can activate the script control by assigning True value to its Active property. This means following:

-Source code will be transferred to specified scripting engine and parsed by it.
-If there were no syntactic errors, all global level code will be immediately executed.
-While script control is active all declared functions remain accessible for calling using TLMDScriptControl.RunProc overloaded methods.

Calling script functions

 

If the script source code declares some functions, they can be called after activating script control using TLMDScriptControl.RunProc method family. For example:

 

VBScript source:

 

Sub SomeVBProc(str)

  MsgBox str

End Sub

 

Calling Delphi code:

 

LMDScriptControl1.Active := True;

LMDScriptControl1.RunProc('SomeVBProc', ['Hellow World!']);

 

Here the arguments array is optional and contains calling arguments. The sophisticated  implementation is used in the LMD-ScriptPack to allow you to specify arguments in direct order (not reverse, as in some other component packs) without performance degradation. The RunProc method returns OleVariant that is a return value of script function (if any). For script procedures that do not return any value you can just skip this result as shown in above example.

 

There is one other way to execute script function. This way uses TLMDScriptControl.Script property of the OleVariant type and utilizes Delphi OLE automation futures:

 

LMDScriptControl1.Active := True;

LMDScriptControl1.Script.SomeVBProc('Hellow World!');

Evaluating expressions

 

Using TLMDScriptControl.Eval method you can evaluate different expressions. For example:

 

var

  v: OleVariant;

begin

  LMDScriptControl1.Active := True;

  v := LMDScriptControl1.Eval('(5 + 7) * 3 – sin(0.1123)');

  ShowMessage(v);

end;

 

Note, that expression must follow used script language syntax. In the expression you can use any built-in the language or declared in script source variables/constants/functions or added external objects.

Since Eval method can be used to evaluate expressions including expressions that use script variables, you can use this method to read script variables values:

 

VBScript source:

 

MyVar = 7

 

Calling Delphi code:

 

var

  v: OleVariant;

begin

  LMDScriptControl1.Active := True;

  v := LMDScriptControl1.Eval('MyVar');

  ShowMessage(v);

end;

Executing statements

 

Use TLMDScripControl.ExecuteStatement method to execute script language statement. The statement must follow used script language syntax. In the statement you can use any language built-in or declared in script source variables/constants/functions or added external objects. You can also call procedures. Since ExecuteStatement method can be used to execute statements including statements that use script variables, you can use this method to write into script variables:

 

VBScript source:

 

MyVar = 7

 

Calling Delphi code:

 

LMDScriptControl1.Active := True;

LMDScriptControl1.ExecuteStatement('MyVar = 9');