Recent

Author Topic: (SOLVED) Pascal Script question  (Read 5402 times)

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
(SOLVED) Pascal Script question
« on: October 11, 2017, 03:51:48 pm »
Hi guys, I made a small example in attachment.

My problem is to allow the pascal script code to interact with program variables. In the example I try to print var1 and var2. But it goes wrong. How do I make it print from Pascal Script the contents of var1 and var2? Thank you
« Last Edit: October 14, 2017, 02:23:07 pm by xinyiman »
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

SunyD

  • Guest
Re: Pascal Script question
« Reply #1 on: October 11, 2017, 06:16:11 pm »
First you can't use var2 in because it is in local procedure.
You must handle TPSScript.OnCompile and TPSScript.OnExecute events.
You had TPSScript.OnCompile, I added TPSScript.OnExecute.

I modified your example and it is working now:
Code: Pascal  [Select][+][-]
  1. //..  <--- This means there is your code
  2.  
  3.  
  4. //..
  5. uses
  6.  //..
  7.   uPSRuntime; // ADD THIS!
  8.  
  9. type
  10.  
  11.   { TForm1 }
  12.  
  13.   TForm1 = class(TForm)
  14.     //..
  15.     var2: string; // ADD !!!
  16.   end;
  17.  
  18. var
  19.   Form1: TForm1;
  20.  
  21. implementation
  22.  
  23. {$R *.lfm}
  24.  
  25. { TForm1 }
  26.  
  27. procedure TForm1.Button1Click(Sender: TObject);
  28. / / remove var2 here
  29. //var
  30.   //var2: string;
  31. begin
  32.  //..
  33. end;
  34.  
  35. procedure TForm1.PSScript1Compile(Sender: TPSScript);
  36. begin
  37.    //..
  38.  
  39.      // NEXT 2 LINES ARE NEW
  40.      Sender.AddRegisteredVariable('var1', 'string');
  41.      Sender.AddRegisteredVariable('var2', 'string');
  42. end;
  43.  
  44. procedure TForm1.PSScript1Execute(Sender: TPSScript);
  45. begin
  46.   // THIS IS NEW
  47.   VSetString(Sender.GetVariable('VAR1'),var1); // in uPSRuntime.pas;
  48.   VSetString(Sender.GetVariable('VAR2'),var2); // in uPSRuntime.pas;
  49. end;
  50.  
  51. //..
  52.  
  53.  
  54.  

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: Pascal Script question
« Reply #2 on: October 11, 2017, 10:15:55 pm »
Thank you, your example works well.
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: (SOLVED) Pascal Script question
« Reply #3 on: October 12, 2017, 08:29:35 am »
Another question. If instead of a string I wanted to use a TZQuery, instead of VSetString, what should I use?
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

SunyD

  • Guest
Re: Pascal Script question
« Reply #4 on: October 12, 2017, 11:36:07 am »
TZQuery is class and unknown for pascalscript. For unknown classes you must create "import/register" unit.
Registered classes you can use:
Code: Pascal  [Select][+][-]
  1. //TStringList is already registered you can use it.
  2. Sender.AddRegisteredVariable('Pars', 'TStringList'); // i declared this  variable: Pars: TStringList;
  3.  

All the plugins you dropped on your form registers various classes, types and component. Look in the units you can learn something.

Make your life easy, do what I did.
Also I am using TZQuery in my pascalscripts. But I don't created register unit. I use TDataSet in scripts.

I created in my form function for script like this:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.ZQueryCreate(var aQ: TDataSet);
  2. begin
  3.   aQ:=TZQuery.Create(Application);
  4.   TZQuery(aQ).Connection:=ZConnection1;
  5. end;
  6.  
  7. procedure  TForm1.ZSQLExec(aQ: TDataSet; aStr: String);
  8. begin
  9.   TZQuery(aQ).Close;  TZQuery(aQ).Sql.Clear;
  10.   TZQuery(aQ).Sql.Add(aStr);
  11.   TZQuery(aQ).ExecSQL;
  12. end;
  13.  
  14. procedure TForm1.PSScript1Compile(Sender: TPSScript);
  15. begin
  16.      Sender.AddMethod(Self, @TForm1.ZQueryCreate,'procedure ZQueryCreate(var aQ: TDataSet);');
  17.      Sender.AddMethod(Self, @TForm1.ZSQLExec,'procedure  ZSQLExec(aQ: TDataSet; aStr: String);');
  18. end;
  19.  


In your script you can use:
Code: Pascal  [Select][+][-]
  1. var ds: TDataSet;
  2. begin
  3.    ZQueryCreate(ds);
  4.   ZSQLExec(ds, 'select * from table1');
  5.   //then you can work with field same way as in freepascal
  6.  ds.first;
  7.  while no ds.eof do begin
  8.    writeln(ds.fieldbyname('field1').asstring);
  9.    ds.next;
  10.  end;
  11.  
  12.   ds.Free;
  13.  end;
  14. end;
  15.  



You can also google for it: :)
https://duckduckgo.com/?q=pascalscript



xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: Pascal Script question
« Reply #5 on: October 13, 2017, 09:00:02 am »
Thank you. Is perfect!  :D
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

 

TinyPortal © 2005-2018