Add Scripting to Your Apps (Views: 100)


Using the Microsoft Script Control


More and more programmers are supporting scripting in their applications. The reason is simple: Scripting gives your users remarkable flexibility, and the power to customize and control an application using a simple scripting language.

Until recently, implementing scripting in an application wasn't for the faint of heart. It involved setting up the scripting engine and worrying about parsing the script, handling errors, etc. Everything changed when Microsoft released the scripting control. It's now trivial to add scripting support to your application.

The Microsoft Script Control enables you to support scripting in your applications. If you've ever marveled at the power and flexibility that VBA adds to Microsoft Word and Excel, then this article is for you. If you haven't, then this article will show you why you should be impressed with scripting.

What You Need

You'll need to download the scripting control from Microsoft. If you don't have Microsoft Internet Explorer 3.01 or later, you'll also need to download one or both of the scripting languages, VBScript, and/or JScript. While you're at the site, you might also want to download the language documentation.

Microsoft states that the scripting control is for small- to medium-sized applications only. This is because the control sacrifices some speed in favor of ease of use. For most of us this is well worth the trade off. Even if you're planning to write a huge application, it would be wise to learn how scripting works first. The scripting control is ideal for this purpose.

One of the fabulous features of scripting is that changing the scripting language is very simple. This means that you can select any scripting language you would like to use. Some other (non-Microsoft) ActiveX scripting languages include PERL and REXX.

Once you've downloaded the scripting control, you need to install it; run the self-extracting .EXE and follow the instructions. If you downloaded the language documentation, now would be a good time to install it.

Next you need to make the control available to Delphi applications. In Delphi, click Component | Import ActiveX Control. Select Microsoft Script Control 1.0 (Version 1.0) from the list of available ActiveX controls (see Figure 1). Click Install and the control should appear on the ActiveX tab.

Figure 1: Importing the script control from the Import ActiveX dialog box.

A Simple Scripted Application

To create a scripted application, you first need to create a new application. Figure 2 shows how I have set up the application's user interface. The Memo component at the top contains the user's script. The script controls the RichEdit component at the bottom. This is a simple application, but it's sufficient to demonstrate how powerful scripting is.

Figure 2: The sample application at design time.

Next you need to create an automation class. This is the class to be added to the scripting control to allow the RichEdit component to be controlled by the user's script. In other words, when the user types RichEdit.DoSomething in the script box, the DoSomething procedure must be provided by an automation class. Don't worry if this doesn't make a whole lot of sense right now; as you work through the article you'll gain a better understanding.

Creating an automation class in Delphi is easy. Select File | New from the menu. Select the ActiveX tab, and double-click the Automation Object icon. For the example application, the class is named TSCTest, so type SCTest in the class name edit box (notice that the leading "T" has been omitted) as shown in Figure 3. You don't need to modify the Instancing or Threading Model options.

Figure 3: Creating a new automation class

After you click OK, the Delphi Type Library editor should be open (see Figure 4). If it isn't, open it by selecting View | Type Library. The Type Library editor is where you add properties and methods to the automation class. As you can see from Figure 4, I've added a few methods and properties to the type library.

Figure 4: Delphi's Type Library editor.

After saving the project, you'll be asked to provide a name for the type library, and then a name for an auto-generated .PAS file. This auto-generated file contains the automation class, TSCTest. This class contains all the methods and properties that were added in the Type Library editor. However, it's missing one crucial thing. There must be a way to link the automation class with the control it will automate. To accomplish this, a constructor has been added that accepts a RichEdit argument as its only parameter:

TSCTest = class(TAutoObject, ISCTest)
constructor Create(_redControl: TRichEdit);
redControl: TRichEdit;

Setting Up the Scripting Control

Select the language you wish to use, and enter it in the "Language" selector. For this application I've chosen VBScript, but you can use JScript, or any other ActiveX scripting language that you've installed. Selecting a language is as simple as that. You must also make certain that your user has the language - either by telling them where to download the language, or by requiring that they have Internet Explorer 3.01 or later, and sticking to VBScript or JScript.

Writing the Code for the Application

Don't forget the call to OLEInitialize, which must always be called before using any OLE functions. Next an instance of the automation class must be created, passing the rich edit as its parameter. Lastly, the automation class must be added to the script control. This is an important step and is what allows the script to "see" the automation class.

You must add the type library unit as well as ActiveX and ComObj to the main unit's uses clause:

procedure TForm1.FormCreate(Sender: TObject);
// We're going to use OLE functions.
// Create automation object for the "control."
SCTest := TSCTest.Create(red_Test);
// Add this object to the scripting control.
ScriptControl1.AddObject('red', SCTest, False);

The code isn't complex; simply call OLEUninitialize. Don't free the TSCTest (automation) class. This is done automatically by Delphi:

procedure TForm1.FormDestroy(Sender: TObject);
// Finished using OLE functions.

This is the event handler that will be called when the Run button is clicked. As you can see below, it's a one-line procedure. A call to ExecuteStatement is made, passing the script in the memo as its only parameter:

procedure TForm1.but_RunClick(Sender: TObject);
// Run the script present in the memo.

The following code shows a snippet from the automation class' code. Notice the get and set functions for the SelText property. The get function is used when the SelText property is used to read the selected text. The set property is used when the SelText property is used to set the selected text:

function TSCTest.Get_SelText: WideString;
Result := redControl.SelText;

procedure TSCTest.Set_SelText(const Value: WideString);
redControl.SelText := Value;

That's all there is to it. Run the application and you can execute some VBScript to control the RichEdit. Here's a sample script:

red.SelText = "123456"
red.SelStart = 0
red.SelLength = 3
red.Bold = True
red.Underline = True
red.SelLength = 0


As you can see, creating a simple, yet powerful, scriptable application takes almost no time. By using the Microsoft Script Control, you can, in a few lines of code, accomplish what was once reserved for only the most complex of applications. This article hasn't covered procedures, events, or any of the other more complex scripting features. However, this should provide a good starting point for your scripted applications.

Component Download: http://www.baltsoft.com/files/dkb/attachment/Add_Scripting_to_Your_Apps.ziphttp://www.baltsoft.com/files/dkb/attachment/Add_Scripting_to_Your_Apps.zip

<< Back to main page