Author Topic: 1.50: scripting first test  (Read 9642 times)

Offline alex

  • Developer
  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2181
  • Karma: +37/-3
    • View Profile
    • HippoEDIT
1.50: scripting first test
« on: July 26, 2011, 03:33:24 am »
Hi,

here I will publish first examples for scripting in HippoEDIT. First no documentation, only examples.
So, interested can check what will come and try it a little bit. But I do not promise that API ill not change until final release of 1.50.

To get brief overview of API function you can use this raw help file (chm file).
To execute scripts you need latest version of 1.50 alpha.
There are two ways to call script currently:
- open script file and use Tools->Execute "Current file" command
- Create a tool, taking script path into command window. It should work as normal tool, and called tool script.
- In next version will be introduced also so called service scripts. They will work based on events registration and can run resident in parallel.

HippoEDIT supports any installed active scripting engine. By default supported  JavaScript and VBScript. If you start any VBS or JS file inside of HippoEDIT with Execute command or as tool, HE will assume that it is HE script and will try to execute passing scripting objects. So it can work with any *.js or *.vbs file. But for convenience there are also dedicated schemes inherited from JS ans  VBS syntaxes and associated with *.hejs and *.hevbs extensions correspondingly. Dedicated schemes extended by HE specific syntax. Till now only HEJS schema is up to date. HEVBS will be updated later.

Script files support includes (see usage of include.js for example). Usage of include files from other supporting scripting language is also supported (you can include JS includes in VBS scripts for example).
« Last Edit: August 17, 2013, 12:24:49 pm by alex »

Offline alex

  • Developer
  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2181
  • Karma: +37/-3
    • View Profile
    • HippoEDIT
Re: 1.50: scripting first test
« Reply #1 on: July 26, 2011, 03:34:17 am »
more scripts
« Last Edit: August 31, 2011, 04:04:03 am by alex »

Offline alex

  • Developer
  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2181
  • Karma: +37/-3
    • View Profile
    • HippoEDIT
Re: 1.50: scripting first test
« Reply #2 on: July 26, 2011, 03:45:27 am »
small VBS example

Offline Stefan

  • Administrator
  • Hero Member
  • *****
  • Posts: 775
  • Karma: +6/-0
    • View Profile
Re: 1.50: scripting first test
« Reply #3 on: July 31, 2011, 07:43:02 pm »
Thank you for your great work Alex!

This looks rather like an complicated syntax. I was more thinking about an simple 'one-line' syntax.
But as every time: if you do it you do it well.  Taking an closer lock the syntax seams as easy as HTML code.


I think with this HE scripting all will be possible.
I mean, other editors have scripting features too, but without an handy UI like you build for HE they are
limited to simple actions mostly and tricky to use for more feature-rich needs. We all know this simple InputBox questions,
most three or four in an row to prompt the user for his options only to script an simple text modification. With HippoEDIT
scripting GUI features the user can build very professional locking (and working) tools for his own needs without HE itself
will become an 'all-features-in-one-app-monster'.

Let's see how long it needs till THIS feature will seen in other editors too like so many in the past.

I can see the power of the programming language style syntax and have to play around to get used to it.

Here are some impressions for those who are not so familiar with scripting or don't want to download and test an alpha build,
 (but don't be afraid, since the scripts are already wrote, your part only is to execute them)
« Last Edit: July 31, 2011, 08:08:03 pm by Stefan »

Offline Stefan

  • Administrator
  • Hero Member
  • *****
  • Posts: 775
  • Karma: +6/-0
    • View Profile
Re: 1.50: scripting first test
« Reply #4 on: August 01, 2011, 10:40:21 am »
My first own dialog (well just copy and paste and a little modified.
ila_rendered


(Note: the alert() at the end of the code  is just non-working dummy code)

Offline alex

  • Developer
  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2181
  • Karma: +37/-3
    • View Profile
    • HippoEDIT
Re: 1.50: scripting first test
« Reply #5 on: August 02, 2011, 03:13:20 am »
Hi Stefan,

if you only want to provide support for input fields and combos, yes it possible to go with "one-line" syntax. But if you want to provide similar level of flexibility as have plugins, it is impossible. Hippoedit provides a lot of controls with a lot of properties with layout possibilities, try to imagine syntax you need to invent for such "one-line" representation. And it should be well formed, with escaping and robust to accept some level of errors. And at the end you need to teach a user of how to use it.
Using XML is standard (for HE), well formed, and fast because already optimized in HE. So it was an  obvious and right choice. I have evaluated it for a long time beforehand.
Example from NotePad it is cryptic nightmare :)

I will try to publish syntax description for XML dialogs, with explanations for all properties.
And I have updated the initial post with raw chm with interface description.

About your examples:
- you (because I know your preferences) can also use VBS for scripting (forgot, the schema is not done for it, but it will work, even then not syntax highlighted). I just have more experience with JS.
- id property for dialog, used for persistence of size and dialog position, that is maybe why layout of your dialog is strange (buttons are not at the bottom)
- you can play with required fields, if they do not filled, "positive" buttons will be disabled. Yon need to use predefined "ok" "apply" as returnvals to make it working.
- you can also mark paragraph as required, than it will have red asterisk.
- for getting results from dialog, you can use as SettingsStorage object (CreateStorage), as automatic way, in which HE reads variables from script by id and updates them on "positive" dialog end.
- if you create a xml dialog, try to use special syntax ('@ - @'), which will enable specialized parser for it.

I will update dialog_actions.hejs with an example.

BR, Alex.
P.S: and thank a lot for a feedback :)
« Last Edit: August 02, 2011, 03:18:48 am by alex »

Offline Stefan

  • Administrator
  • Hero Member
  • *****
  • Posts: 775
  • Karma: +6/-0
    • View Profile
Re: 1.50: scripting first test
« Reply #6 on: August 02, 2011, 10:34:46 am »
Alex>About your examples:
Alex>- you (because I know your preferences) can also use VBS for scripting
I try to use (learn) JS too.


Alex>- id property for dialog, used for persistence of size and dialog position,
I do not see any mistake i have done. What should i change?

Alex>- you can play with required fields, if they do not filled, "positive" buttons will be disabled.
Alex>- you can also mark paragraph as required, than it will have red asterisk.
Alex>- if you create a xml dialog, try to use special syntax ('@ - @'), which will enable specialized parser for it.
Yes, already seen.

Alex>Yon need to use predefined "ok" "apply" as returnvals to make it working.
"My" returnvals are "Ok" and "Cancel" as copied from your script. Only i changed the button labels (title).
What do you mean? Have i make it wrong?

Alex>- for getting results from dialog, you can use as SettingsStorage object (CreateStorage), as automatic way,
Alex> in which HE reads variables from script by id and updates them on "positive" dialog end.
How? How can i get the value of ID "itb" ?

Code: Javascript
  1. var myDialog = '@<dialog title="Insert text" id="test"> \
  2. <paragraph text="Enter the text to be inserted before and/or after each selected line"/><spacer/> \
  3.   <group><paragraph text="Before:" minwidth="6" align="left"/><edit id="itb" minwidth="35"/></group> \
  4.   <group><paragraph text="After:" minwidth="6" align="left"/><edit id="ita" minwidth="35" /></group> \
  5.   <group uniform="true" align="right|bottom"> \
  6.            <button title="&amp;Lets go" returnval="ok" default="true"/> \
  7.            <button title="&amp;No thanks" returnval="cancel"/> \
  8.   </group> \
  9. </dialog>@';
  10.  
  11. // write something to standard output:
  12. var HEOutput = Application.Output();
  13. HEOutput.writeln("HippoEDIT version: " + Application.Version);
  14.  
  15. // get the user input into "varMyOutputStorage " and write to output "HEOutput":
  16. var varMyOutputStorage = CreateStorage();
  17. HEOutput.writeln("My dialog returns: " + dialog(myDialog, varMyOutputStorage));
  18.  

My output:
Code: Text
  1. HippoEDIT version: 1.50.721
  2. My dialog returns: 1
  3.  
« Last Edit: August 03, 2011, 02:44:28 am by alex »

Offline JJK

  • Registered User
  • Full Member
  • *
  • Posts: 152
  • Karma: +2/-0
    • View Profile
Re: 1.50: scripting first test
« Reply #7 on: August 02, 2011, 10:20:25 pm »
Hi all
I downloaded HeApi.chm but I can't read it under Windows 7.
I had installed Windows6.1-KB917607-x86.msu from Microsoft which is designed to read .hlp files under Windows 7, but it does not work.

Any idea ?
TIA

Offline Stefan

  • Administrator
  • Hero Member
  • *****
  • Posts: 775
  • Karma: +6/-0
    • View Profile
Re: 1.50: scripting first test
« Reply #8 on: August 02, 2011, 11:04:05 pm »
I downloaded HeApi.chm but I can't read it
1. Right click on the file, and click on Properties.
2. Under the General tab,
click on the Unblock button beside the message, "This file came from another computer and might blocked to help protect  this computer."
and click on OK.

Offline alex

  • Developer
  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2181
  • Karma: +37/-3
    • View Profile
    • HippoEDIT
Re: 1.50: scripting first test
« Reply #9 on: August 03, 2011, 03:38:36 am »
Hi Stefan,

id="test" - I noticed that your dialog is bigger than content and buttons are not aligned to bottom, this is or a bug, or you have used same id for a dialog with bigger size, and size was stored ans used for all dialogs with same id. And because dialog was not marked as resizable, there is no way to make it smaller (this is maybe a bug, I will check).

Quote
"My" returnvals are "Ok" and "Cancel" as copied from your script. Only i changed the button labels (title).
What do you mean? Have i make it wrong?
Now I see, they are fine. Generally you can use as hard-coded enumerations, as any integer number there. Values from -1 till 9 are mapped to enumerations. Some enumerations are positive (as I mentioned "ok", "yes"), and there are some built-in extras for them, as auto-disable by missing required fields and auto apply of script variables on finish of the dialog using them.

Quote
How? How can i get the value of ID "itb" ?
There are two ways: implicit and explicit.
 - implicit:
add this code before calling of the dialog (line 17).
Code: Javascript
  1. var itb="/*";
  2. var ita="*/";
you do not need to assign values, they are optional and just read before and used as defaults.
With such code after start of your dialog you will have fields filled with "/*" and "*/" and if you will select "ok" button, modified values will be copied back.
There is one important point about such implicit way - it works ONLY for GLOBAL variables. For vars from withing functions, for example, it will not work.

- explicit:
Works always, and requires usage of settings storage object, the same way as you do if you want to save script values between sessions or share them between scripts.
Add this code before calling of the dialog:
Code: Javascript
  1. var varMyOutputStorage = CreateStorage();
  2. varMyOutputStorage.write("itb", "/*");
  3. varMyOutputStorage.write("ita", "*/");
  4.  
and this after calling of the dialog. Variables will be updated in any case, independent from return code. This is your job to check return code and read or not read variables back:
Code: Javascript
  1. var varMyOutputStorage = CreateStorage();
  2. var itb = varMyOutputStorage.read("itb");
  3. var ita = varMyOutputStorage.read("ita");
  4.  

if you do not want to initialize variables before, you can skip first part, and only read variables from storage later. They will be created automatically in the map, based on id properties in dialog definition.

P.S: I have updated your message with syntax highlighting for you code snippet. I thought you have already seen this - check toolbar. Also now in-line images are supported.


Offline Stefan

  • Administrator
  • Hero Member
  • *****
  • Posts: 775
  • Karma: +6/-0
    • View Profile
Re: 1.50: scripting first test
« Reply #10 on: August 03, 2011, 10:21:09 am »
Alex> id="test" - I noticed that your dialog is bigger
That was the only dialog and the only ID "test" in that script.
But today the dialog looks normal, as expected.
HE has waited for me over night with that script open, and i didn't modified the dialog,..
only the output code as your reply shows, started the script and... the buttons are at the bottom now.

ila_rendered


- - -

Thanks for your explanation, i got it working:

- implicit

Code: Javascript
  1. // initialize the output vars, and set default values if wanted:
  2. var ID_itb="not used by user";
  3. var ID_ita="";
  4.  
  5. // build up your dialog and store it in var vMyDialog:
  6. var vMyDialog = '@<dialog title="Insert text" id="test"> \
  7. <paragraph text="Enter the text to be inserted before and/or after each selected line"/><spacer/> \
  8.   <group><paragraph text="Before:" minwidth="6" align="left"/><edit id="ID_itb" minwidth="35"/></group> \
  9.   <group><paragraph text="After:" minwidth="6" align="left"/><edit id="ID_ita" minwidth="35" /></group> \
  10.   <group uniform="true" align="right|bottom"> \
  11.            <button title="&amp;OK" returnval="ok" default="true"/> \
  12.            <button title="&amp;Cancel" returnval="cancel"/> \
  13.   </group> \
  14. </dialog>@';
  15.  
  16. // execute the dialog:
  17. var vErrorcode = dialog(vMyDialog);
  18.  
  19. // if [OK] is clicked...
  20. if (vErrorcode==1){
  21.         // write to HEs standard output:
  22.         var vHEOutput = Application.Output();
  23.         vHEOutput.clear();
  24.         vHEOutput.writeln("HippoEDIT version: " + Application.Version);
  25.         vHEOutput.writeln("Returned error code: " + vErrorcode);
  26.         vHEOutput.writeln("Text to be inserted before: " + ID_itb);
  27.         vHEOutput.writeln("Text to be inserted after: " + ID_ita);
  28. }
  29.  

Execute this script and just pressing [OK] results into:
Code: [Select]
HippoEDIT version: 1.50.721
Returned error code: 1
Text to be inserted before: not used by user
Text to be inserted after:




- explicit

Code: Javascript
  1. // build up your dialog and store it in var vMyDialog:
  2. var vMyDialog = '@<dialog title="Insert text" id="test"> \
  3. <paragraph text="Enter the text to be inserted before and/or after each selected line"/><spacer/> \
  4.   <group><paragraph text="Before:" minwidth="6" align="left"/><edit id="ID_itb" minwidth="35"/></group> \
  5.   <group><paragraph text="After:" minwidth="6" align="left"/><edit id="ID_ita" minwidth="35" /></group> \
  6.   <group uniform="true" align="right|bottom"> \
  7.            <button title="&amp;OK" returnval="ok" default="true"/> \
  8.            <button title="&amp;Cancel" returnval="cancel"/> \
  9.   </group> \
  10. </dialog>@';
  11.  
  12. // create new instance of an settings storage object:
  13. var vMyOutputStorage = CreateStorage();
  14. // set default values (if wanted):
  15. vMyOutputStorage.write("ID_itb", "default value for \"before\"");
  16. //vMyOutputStorage.write("ID_ita", "default value for \"after\"");
  17.  
  18. // execute the dialog and use OutputStorage to store the user input:
  19. var vErrorcode = dialog(vMyDialog, vMyOutputStorage);
  20.  
  21. // if [OK] is clicked...
  22. if (vErrorcode==1){
  23.         // get the user input:
  24.         var vITB = vMyOutputStorage.read("ID_itb");      if (vITB==""){vITB="'Before' is not used by user";}
  25.         var vITA = vMyOutputStorage.read("ID_ita");      if (vITA==""){vITA="'After' is not used by user";}
  26.  
  27.         // write to HEs standard output:
  28.         var vHEOutput = Application.Output();
  29.         vHEOutput.clear();
  30.         vHEOutput.writeln("HippoEDIT version: " + Application.Version);
  31.         vHEOutput.writeln("Returned error code: " + vErrorcode);
  32.         vHEOutput.writeln("Text to be inserted before: " + vITB);
  33.         vHEOutput.writeln("Text to be inserted after: " + vITA);
  34. }
  35.  


Execute this script and just pressing [OK] results into:
Code: [Select]
HippoEDIT version: 1.50.721
Returned error code: 1
Text to be inserted before: default value for "before"
Text to be inserted after: 'After' is not used by user

Where is the thump-up smiley?  ;D
« Last Edit: August 03, 2011, 10:36:39 am by Stefan »

Offline JJK

  • Registered User
  • Full Member
  • *
  • Posts: 152
  • Karma: +2/-0
    • View Profile
Re: 1.50: scripting first test
« Reply #11 on: August 03, 2011, 05:41:11 pm »
1. Right click on the file, and click on Properties.
2. Under the General tab,
click on the Unblock button beside the message, "This file came from another computer and might blocked to help protect  this computer."
and click on OK.
Many thanks. It works well now.
I didn't know that feature :( but now I know it :) 

Offline Ramon

  • Registered User
  • Newbie
  • *
  • Posts: 10
  • Karma: +0/-0
    • View Profile
Re: 1.50: scripting first test
« Reply #12 on: August 04, 2011, 01:14:11 am »
I just playin with this new feature.

When I use csript with script like bellow I do not see CMD window. But if I use adapted script for HE and run "Tools->Execute Test.js", CMD window has appear. What I need to do to hide CMD window.

Script for cscript:
Code: Javascript
  1. var WshShell = new ActiveXObject("WScript.Shell");
  2. var WshExec = WshShell.Exec("cmd /c dir c:\\");
  3.  
  4. while (!WshExec.StdOut.AtEndOfStream)
  5.         WScript.Echo( WshExec.StdOut.ReadLine() );
  6.  

Script for HE:
Code: Javascript
  1. var Output = Application.Output();
  2. var WshShell = new ActiveXObject("WScript.Shell");
  3. var WshExec = WshShell.Exec("cmd /c dir c:\\");
  4.  
  5. Output.clear();
  6.  
  7. while (!WshExec.StdOut.AtEndOfStream)
  8.         Output.writeln( WshExec.StdOut.ReadLine() );
  9.  
« Last Edit: August 04, 2011, 02:04:25 am by alex »

Offline alex

  • Developer
  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2181
  • Karma: +37/-3
    • View Profile
    • HippoEDIT
Re: 1.50: scripting first test
« Reply #13 on: August 04, 2011, 02:20:20 am »
Hi Ramon,

It does not work with first example, because HE does not know what to do with  WScript.Echo :) You send output to WScript and where it sends it further I have no clue :) But OK, maybe in this case I also need to catch standard output. The only problem, is it can conflict with usage of unnamed Output (same pane) from withing the script. But maybe that is fine. I will check.

About second example. I think this is too complicated :) Check scripting.hejs for example:
Code: Javascript
  1. // capture cmd.exe and execute a command
  2. var output_cmd = Application.Output("CMD");
  3. output_cmd.Capture("cmd.exe", "", "exit");
  4. Application.sleep(1000);
  5. output_cmd.AddCommand("dir");
  6. Application.ClosePane("", output_cmd);

There is also a way to just run tool in background and capture the standard output in string variable:
Code: Javascript
  1. var sResult = CaptureOutput("test.bat");
See attached heapi.chm for parameters.

If you close standard pane, it is just hidden. If you close custom pane (named output fex), it is really closed.
For now to way to just hide it. Maybe I need to add this, but not yet done.

BR, Alex
« Last Edit: August 04, 2011, 02:27:03 am by alex »

Offline Stefan

  • Administrator
  • Hero Member
  • *****
  • Posts: 775
  • Karma: +6/-0
    • View Profile
Re: 1.50: scripting first test
« Reply #14 on: August 04, 2011, 10:11:25 am »
Alex, the first script from Ramon was an example only, tested outside of HE by involving script.exe.

Common info:
WScript is an object of the Windows Scripting Host, meaning only available by using cScript.exe or wScript.exe
Since HippoEDIT is it own scripting host, the WScript object is not involved at all.


An other idea for
var WshExec = WshShell.Exec("cmd /c dir c:\\");

maybe
var WshExec = WshShell.RUN( strCommand, intWindowStyle, bWaitOnReturn);
where intWindowStyle could be '0' (Hides the window)

See
http://msdn.microsoft.com/en-us/library/d5fk67ky%28v=vs.85%29.aspx

 

Related Topics

  Subject / Started by Replies Last post
18 Replies
8530 Views
Last post June 17, 2016, 11:09:59 pm
by alex
2 Replies
4911 Views
Last post October 07, 2009, 01:50:53 pm
by alex
8 Replies
5751 Views
Last post November 18, 2009, 03:35:00 am
by samuel1991
3 Replies
2621 Views
Last post November 30, 2011, 12:20:59 am
by alex
2 Replies
2088 Views
Last post July 07, 2013, 03:09:16 pm
by RJP74