ActiveX Scripting Components Version 1.07 |
|
The ActiveX Script
Components is a small collection of native Delphi VCL components that
are designed to make adding scripting to your programs as easy as possible.
The components are compatible with Delphi 3, Delphi 4, Delphi 5, and Delphi
6. There are three components in this collection:
TawScriptControl is the main
component in the collection. It is a wrapper component for the Microsoft Script
ActiveX Control. This is a free ActiveX control from Microsoft that provides
basic access the any ActiveX scripting language. Microsoft provide VBScript and
JScript as their standard scripting languages, but many other languages are
available including PerlScript and Python. TawScriptEditor is a dialog component that displays a basic editor. It provides a status bar along the bottom as well as an edit box for specifying the script's language. It does not have any advanced features like syntax highlighting. TawScriptErrorDlg is another dialog component. It looks basically like a MessageDlg, but it understands the errors returned by TawScriptControl and can display those errors in a properly formatted style. There are also a few other objects that are used with these components, such as TawScriptError which is used by TawScriptControl when a scripting error occurs. An example program is distributed with this collection, which gives a brief, but complete example of using all the components. The full source code is also included for all the components and forms. In order to use these components, the Microsoft Script Control must have been installed. It would also be useful if the Microsoft scripting languages, VBScript and JScript, have been installed as well. Windows 2000 and XP include all of these. For previous versions of Windows, all three of these can be downloaded from http://msdn.microsoft.com/scripting/. Version 5 of VBScript and JScript come in one install, which also includes the Windows Scripting Host. The Microsoft Script Control is distributed in a separate install. These installs are also available on my web site, along with the latest version of the AW Scripting Components. The address is http://www.somethingmore.net/scripting/. |
Firstly, the Microsoft
Script Control must be installed. At
http://msdn.microsoft.com/scripting/
you can download this, along with JScript and VBScript. To install the Delphi components, follow these steps:
|
TawScriptControlErrorEvent = procedure (Sender: TObject; Error: TawScriptError) of object; TawScriptControl = class(TComponent) public constructor Create(AOwner: TComponent); override; destructor Destroy; override; function CallFunction(const FunctionName: string; const Params: array of Variant): OleVariant; procedure Clear; property ActiveXScriptControl: TScriptControl; published property AllowUI: boolean; property AutoObjects: TawAutoObjects; property Code: TStrings; property Language: string; property Timeout: integer; property OnError: TawScriptControlErrorEvent; property OnTimeout: TNotifyEvent; property OnCallFunction: TawScriptControlCallEvent; end;TawScriptControl is the main component in the collection. It is a wrapper component for the Microsoft Script ActiveX Control. It can be used with any ActiveX scripting language, such as VBScript, JScript, PerlScript and Python. It also allows OLE Automation objects to be added, which can then be controlled by the scripts. If the Microsoft ActiveX Script Control is not installed, Create will raise an EOleCtrlError exception. function CallFunction(const FunctionName: string; const Params: array of Variant): string; CallFunction will run the procedure in the script with the name specified by FunctionName. Any values in the Params array will be passed to this procedure as it's parameters. function Clear; Clears the Code and AutoObjects properties. property ActiveXScriptControl: TScriptControl; This property gives direct access to the underlying ActiveX Script Control. For advanced users. property AllowUI: boolean; Corresponds to the AllowUI property of the Microsoft Script Control. It specifies whether a script, or the Script Control, is allowed to display user-interface elements, such as dialog boxes. property AutoObjects: TawAutoObjects; AutoObjects is used to specify a collection of OLE Automation objects that are accessible from the scripting engine. When adding or changing the contents of AutoObjects, it's BeginUpdate and EndUpdate methods should be used. Call BeginUpdate before making changes and EndUpdate once the changes are finished. property Code: TStrings; Code contains the actual script code. A procedure must be specified in the code before using the CallFunction method. property Language: string; Language specifies what language the script code is written in. property Timeout: integer; Corresponds to the Timeout property of the Microsoft Script Control. It specifies the time, in milliseconds, after which a user is presented with the option to discontinue scripting code execution. property OnError: TawScriptControlErrorEvent; This event is triggered whenever an error occurs during execution of a script. A TawScriptError object is passed to this event giving details of the error. property OnTimeout: TNotifyEvent; This event is triggered when the time specified in the Timeout property has elapsed and a user has selected End in the resulting dialog box. property OnCallFunction: TawScriptControlCallEvent; This event is triggered whenever the CallFunction method is run. The paramters to CallFunction are passed to the event.Examples: With the Language set to 'JScript' and the code set to:function main(msg) { return msg; }The following code will then run this function:var l_res: string; . . . l_res := ScriptControl.CallFunction('main', ['Hello world!']); ShowMessage(l_res); |
![]() TawScriptEditor = class(TComponent) public constructor Create(AOwner: TComponent); override; destructor Destroy; override; procedure Show; function ShowModal: integer; property IsOpen: boolean; published property Caption: string; property Code: TStrings; property EditorFont: TFont; property Font: TFont; property Height: integer; property Language: string; property Width: integer; property OnChange: TNotifyEvent; end;TawScriptErrorDlg is a dialog component. It consists of two parts, the main script editing area and a status bar running along the bottom. The status bar also contains an edit box which is used for specifying the language the script is written in. Standard keyboard short-cuts are available in the editor, such as copy, paste and select all. The escape key can be used to close the window. Tabs are four characters wide, but they only count as one character for the position shown in the status bar. This is because the Microsoft Script Control counts tabs as one character in it's error messages. The Code and Language properties can be set before opening the editor to give them default values. When the editor is closed, the new values can be read. procedure Show; Show will display the editor, but it will not be modal. When the editor is closed, the OnChange event will trigger if any changes have been made. The IsOpen property is also useful when the editor is not modal. function ShowModal: integer; ShowModal displays the editor as a modal form. When the editor is closed, this function returns with a ModalResult. If changes have been made, it returns mrOK, otherwise mrCancel. The OnChange event will also be triggered if changes have been made. property IsOpen: boolean; If the editor window is open, IsOpen will be True, otherwise it is False. property Caption: string; Caption determines the caption that appears in the title bar of the editor. property Code: TStrings; Code contains the actual script code for the editor. Set this before opening to default the code. Read it after the editor has closed to retrieve the new value. property EditorFont: TFont; This specifies the font to be used for the editing area of the editor. property Font: TFont; This specifies the font to be used for the whole editor window, except the actual editing area. property Height: integer; The height in pixels of the editor window. property Language: string; This contains the language that the script is written in. Set this before opening to default the language. Read it after the editor has closed to retrieve the new language. property Width: integer; The width in pixels of the editor window. property OnChange: TNotifyEvent; This event is triggered whenever the editor window is closed and the code or language have been changed. Examples: // a button click which opens the script editor procedure TForm1.EditButtonClick(Sender: TObject); begin ScriptEditor.Code := ScriptControl.Code; ScriptEditor.Language := ScriptControl.Language; if ScriptEditor.ShowModal = mrOK then begin ScriptControl.Code := ScriptEditor.Code; ScriptControl.Language := ScriptEditor.Language; end; end; |
![]() TawScriptErrorIcon = (eiInfo, eiError); TawScriptErrorDlg = class(TComponent) public constructor Create(AOwner: TComponent); override; destructor Destroy; override; procedure ShowModalMsg(Msg: string; Icon: TawScriptErrorIcon); procedure ShowModalError(Error: TawScriptError); published property Caption: string; property ErrorFont: TFont; property Font: TFont; property Height: integer; property Width: integer; end;TawScriptErrorDlg is a dialog component. It looks similar to a MessageDlg, but it also understands the errors returned by TawScriptControl and can display those errors in a properly formatted style. procedure ShowModalMsg(Msg: string; Icon: TawScriptErrorIcon); ShowModalMsg will display the dialog as a modal form. The Msg parameter will be displayed in the message area of the dialog and the Icon parameter determines which icon is displayed in the top left corner. procedure ShowModalError(Error: TawScriptError); ShowModalError is displays the dialog as a modal form. The message are will contain a formatted error message based on the Error parameter passed to it. It will also display the eiError icon. property Caption: string; Caption determines the caption that appears in the title bar of the dialog box. property ErrorFont: TFont; ErrorForm determines what font is used in the message area of the dialog box. property Font: TFont; This specifies the font to be used for the whole dialog box, except the message area. property Height: integer; The height in pixels of the dialog box. property Width: integer; The width in pixels of the dialog box. Examples: // OnError handler for a ScriptControl procedure TForm1.ScriptControlError(Error: TawScriptError); begin ScriptErrorDlg.ShowModalError(Error); end; // example of using ShowModalMsg ScriptErrorDlg.ShowModalMsg('This is a sample message', eiInfo); |
TawScriptError = class(TObject) public property Column: Integer; property Description: WideString; property HelpContext: Integer; property HelpFile: WideString; property Line: Integer; property Number: Integer; property Source: WideString; property Text: WideString; end;TawScriptError is a simple class which define a number of read-only properties. It is passed in to the OnError event handler on the TawScriptControl component. It is also passed to the ShowModalError procedure of the TawScriptErrorDlg class. The properties correspond to the properties of the Error object in the Microsoft Script Control. See the Microsoft Script Control help file for details. |
TawAutoObjects = class(TCollection) public function Add: TawAutoObject; property Items[Index: Integer]: TawAutoObject; default; end;TawAutoObjects is inherited from the TCollection object. It has had a few, simple overrides and additions to handle a collection of TawAutoObject objects. The AutoObjects property of the TawScriptControl class is of type TawAutoObjects. It allows for a collection of scriptable objects to be defined. Here is an example of filling the collection: var AutoObjectItem: TawAutoObject; SampleAutoObj: TSampleScriptableObj; . . . // create the auto object SampleAutoObj := TSampleScriptableObj.Create; // update the AutoObjects collection ScriptControl.AutoObjects.BeginUpdate; AutoObjectItem := ScriptControl.AutoObjects.Add; AutoObjectItem.AutoObjectName := 'sobj'; // setup the new item AutoObjectItem.AutoObject := SampleAutoObj; // ScriptControl.AutoObjects.EndUpdate; |
TawAutoObject = class(TCollectionItem) public property AutoObject: IDispatch; published property AutoObjectName: string; end;TawAutoObject is a simple enhancement of the TCollectionItem class. It adds two properties, an IDispatch and it's name. See the TawScriptControl for more information on how these are used. |
© 1999-2002 Alexander Wingrove. Email: awingrove@somethingmore.net