Home Home Archives About red box

Filed Under Delphi
digg Javascript in Desktop Applications
visits: 328 | score: 3.7 
posted by sysrpl on Sunday February 6, 2011 7:53 PM

ver at stackoverflow someone asked, "Is it possible to utilize javascript in the making of windows desktop applications". Why yes, not only is it possible, but it's actually quite powerful. In this brief article I describe how you can add script support to your Windows desktop projects.

At the bottom of this page you'll find a Delphi source code package and example application demonstrating how to add javascript support to your Windows desktop applications. Feel free to translate this package to your favorite language.

To get started, create a new Windows Forms VCL application, add ScriptIntf to you uses clause, and create an instance of TJavaScript in your form create method. You have the option to hook up Scriptlet objects, which allows your javascript to access Delphi methods and properties.

procedure TSomeForm.FormCreate(Sender: TObject);
  // Create our javascript engine
  JavaScript := TJavaScript.Create(Self);
  // Make this form available to javascript under the name 'Form'
  FScript.AddScriptlet(CreateComponentScriptlet(Self, 'Form'));
  // Add hosting support including our Alert and Echo methods
  FScript.AddScriptlet(CreateHostScriptlet(ScriptAlert, ScriptEcho));

To add source code to a script, assign text to the lines property. To connect a script call the Execute method:

procedure TSomeForm.ExecuteButtonClick(Sender: TObject);
  JavaScript.Lines := SourceMemo.Lines;

When javascript is connected, it can optionally access your Delphi scriptlets by name:

/* Example hosted javascript */

/* See ScriptIntf.pas for THostScriptlet's methods */
Host.Echo("Hello from javascript");  

/* See ScriptIntf.pas for TComponentScriptlet's properties */
Host.Alert("Save button anchors are: " + Form.SaveButton.Anchors);
Form.ExecuteButton.Caption = "Hello";

The results of the script are shown to the left. Notice that the script was able to set the caption property of our execute button, and access the anchors of our save button. The script was also able to access our Host's Alert and Echo methods.

By writing your own scriptlets objects, you can define any number of objects with any number of methods or properties that can be accessed by scripts. See the ScriptIntf.pas to understand how THostScriptlet and TComponentScriptlet are working in this example.

You can also do the reverse of scriptlets, connecting to a script accessing javascript objects or call javascript functions from Delphi. To access these javascript items you can write code like this:

/* Example hosted javascript */

var a = 12;

function inverse(value) {
  return 1 / value;

And in Delphi you would write:

procedure TSomeForm.Test;
  Script, A: OleVariant;
  if JavaScript.State = ssConnected then
    Script := JavaScript.Script;
    A := Script.a;
    A := 0.25;

download Download the compiled script test project with source code here

print send topic Rate this article  

What is the reason that the source file CODEBOT.INC is missing from the zip file?
Last modified on 10/26/2012 3:39 AM
reply quote
cbknarf said on Friday October 26, 2012 3:39 AM


image link indent align right align middle align left quote underline bold code quote

page generated in 1.014 seconds | last modified 2/06/2011 8:35 PM
none  none