DelpWebScript

DWS Basics

Homepage
DWS
DWS Basics
 

First Step

To use DelphiWebScript in your Delphi project follow these steps:

  • Install the DelphiWebScript component in Delphi
  • Place a TDelphiWebScript component on a form of your project.
  • Assign a script program to the component's text property:
    DelphiWebScript1.Text := 'send ("hello world");';
  • Call the method execute of the component:
    DelphiWebScript1.Execute;
  • Display the compiler messages (if any):
    for x := 0 to DelphiWebScript1.Msgs.Count - 1 do
    ShowMessage(DelphiWebScript1.Msgs[x].AsInfo);
  • Display the results of the execution:
    ShowMessage (DelphiWebScript1.Result);
    This should display the message "Hello World".

Writing Script Programs

DWS programs use almost the same syntax as Delphi programs. The most important difference is that there is no need for variable declaration:

a := 2;
send (a);

You can use IF statements and FOR, WHILE and REPEAT-UNTIL loops as known from Delphi:

if true then send (1) else send (2);

for x := 1 to 10 do send (x);

x := 10;
while x > 0 do dec (x);

x := 0;
repeat inc (x) until x > 10;

Of course it's also possible to create blocks of statement's using BEGIN-END:

for x := 0 to 10 do
begin
send ('Hello');
send (x, x +1);
end;

Variables and Datatypes

If you like to use a variable - e. g. x - in your script the only thing you have to do is to assign a value to x:

x := 1;

The first value you assign to a variable determines the type. The assignment in the expample creates a variable x of type integer with value 1.

Possible datatypes are integer, float, string and boolean:

x := 1; // integer
y := 2.3 // float
z := 1.0 // float
s := 'hello' // string
tf := true;// boolean
tf := false; // boolean

If you use uninitialized variables in your script the compiler shows an error message:

if x = 0 then send (y);

The compile doesn't know anything about the types of x and y. The compiler also shows an error message if you try to assign incompatible values to a variable:

a := 2;
a := 3.0; // Cannot assign float to an integer variable

s := 'Hello';
s := true // Cannot assign a boolean to a string variable

To create a variable of type float you have to use the point - notation even if it's not necassary:

f := 0.0;
instead of
f := 0;

Constants

It's also possible to declare constants in a DWS program. Unlike Delphi it's possible to declare constants everywhere in a script program, even in a loop:

const h = 'hello';
x := 10;
while x > 0 do begin
dec (x);
const a = 2;
end;

Include Files

It's also possible to include script code located in an external file. To include a external file use this syntax:

{$I 'filename'}
...or...
{$Include 'filename'}

If you don't use a file extension DWS adds ".dws". In difference to delphi you always have to put the filename in quotes. DWS looks for the include file in the path:

TDelphiWebScript.IncludePath.