Accessing Host Application Objects from Scripts

Top  Previous  Next

You can add external objects that support IDispatch interface to the script global namespace by using TLMDScriptControl.AddObject method family.

 

The significant advantage of LMD-ScriptPack is the possibility of using almost any Delphi object in the script. Importing and using Delphi objects will be described bellow, here is a simple example of adding a TEdit object to the script execution environment.

 

VBScript source:

 

Sub MainProc

  MyEdit.Text = “Hellow World!”

End Sub

 

Calling Delphi code:

 

LMDScriptControl1.AddObject('MyEdit', TEdit_sw.ToVar(Edit1));

LMDScriptControl1.Active := True;

LMDScriptControl1.RunProc('MainProc');

 

You can also add access to any OLE automation server to the script. The server instance can be, for example, created by CreateOleObject Delphi function:

 

VBScript source:

 

Sub ShowWord

  MyWord.Visible = True

End Sub

 

Calling Delphi code:

 

LMDScriptControl1.AddObject('MyWord', CreateOleObject('Word.Application'));

LMDScriptControl1.Active := True;

LMDScriptControl1.RunProc('ShowWord');

 

The example creates an instance of Microsoft Word application and adds this instance to the script, naming it “MyWord”. After that it becomes accessible to the script code, and script procedure ShowWord makes the application visible by assigning its Visible property.

 

You can also create a reference to OLE object directly from the script code. Different script languages uses different function to do this, refer to the language manual; for example, VBScript (as well as NativeVb) uses CreateObject function:

 

VBScript source:

 

Sub ShowWord

  Set MyWord = CreateObject("Word.Application")

  MyWord.Visible = True

End Sub

 

Calling Delphi code:

 

LMDScriptControl1.Active := True;

LMDScriptControl1.RunProc('ShowWord');

 

The example utilizes already existing OLE automation server, but you can create and register in OS your own server using standard Automation Object Wizard from Delphi File|New menu.