Using Server Scriptlets in Delphi

In Chapter 8 we talked about the limited support that the Microsoft Scriptlet Control provides outside the Visual Basic and the IE4 environments. In particular, we pointed out that DHTML Scriptlets couldn't be successfully used in Delphi programs due to certain scriptlet control shortcomings.

Server Scriptlets, instead, are ready to be used in Delphi since they are real COM objects. No matter what the run-time module does behind the curtains. What other applications see is a COM object. And what's important is that you can write it in JavaScript!

To demonstrate this, let's see how our Date Calculator scriptlet works in a Delphi 3 program.

The key instruction is the creation of the COM automation object This requires a quite similar approach to that in Visual Basic.

g_sctDateCalc := CreateOleObject('DateCalculator.Scriptlet');

The full source code is the following:

unit d3sslet;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, OleAuto;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    Label1: TLabel;
    Edit2: TEdit;
    Label2: TLabel;
    Edit3: TEdit;
    Button2: TButton;
    Label3: TLabel;
    Edit4: TEdit;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  g_sctDateCalc: Variant;

implementation

{$R *.DFM}

{ Button: Current Date }
procedure TForm1.Button1Click(Sender: TObject);
begin
  g_sctDateCalc.Separator := Edit4.Text;
  ShowMessage( g_sctDateCalc.DateAsDDMMYYYY() );
end;

{ Button: Calculate }
procedure TForm1.Button2Click(Sender: TObject);
begin
  Edit3.Text := g_sctDateCalc.Add( Edit1.Text, StrToInt(Edit2.Text) ); 
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Edit1.Text := 'September 23, 1995';
  Edit2.Text := '1000';
  Edit3.Text := '';
  g_sctDateCalc := CreateOleObject('DateCalculator.Scriptlet');
end;

end.

The entire Delphi 3 project is available from our site. The file to download is d3sslet.zip. With Delphi, we get the same strange behavior we have in Visual Basic when calling methods with no parameters.

© 1997 by Wrox Press. All rights reserved.