Sparx Systems Forum

Enterprise Architect => Bugs and Issues => Topic started by: Guillaume on January 20, 2022, 02:48:37 am

Title: EA16 JavaScript DLGInputBox bug
Post by: Guillaume on January 20, 2022, 02:48:37 am
Hello,

I'm using JavaScript to create EA scripts. I need to prompt the user via an input popup that must be prepopulated with an initial value. Using EA 16 new libraries (EAScriptLib.JavaScript-Dialog), I tried the following:

var userInput = DLGInputBox("Question","Title","Default");

Unfortunately I'm getting a "vbe.eval is not a function" error.
Note that Session.Input works but it doesn't let me specify a default value.
Is there a workaround e.g. a suitable JavaScript function to use?

thanks
Title: Re: EA16 JavaScript DLGInputBox bug
Post by: Sunshine on February 09, 2022, 06:29:39 am
Sorry not tried it, just used session.input in past. Just wondering if its worth doing a bit of a test with the VB version to see if that works then try the JScript to see if that works. The JScript code looks identical. Just thinking if the VB version works you know the original works. Then trying the JScript one gets you one step closer. It could be something to do with the script engine underneath running that code.  Dunno just a thought.
I'd seek help from Sparx support if I were you should you not get an answer in a couple of days.
Title: Re: EA16 JavaScript DLGInputBox bug
Post by: Guillaume on February 10, 2022, 05:41:39 am
It works fine both with JScript and VBScript. I contacted Sparx support.

Thanks
 
Title: Re: EA16 JavaScript DLGInputBox bug
Post by: Guillaume on May 18, 2022, 04:30:03 pm
Hi,

Using the latest EA16 build 1604, I tried to use the DLGInputBox function from EAScriptLib.JavaScript-Dialog with the simple Javascript below.
I'm still getting the following error:

EAScriptLib.JavaScript-Dialog error: Exception in COMObject::Create
Classe non enregistrée, Line:26


Code: [Select]
!INC Local Scripts.EAConstants-JScript
!INC EAScriptLib.JavaScript-Dialog

function main()
{
var userInput = DLGInputBox("prompt","titre","default value");
Session.Output(userInput);
}

main();

Is there an alternative Javascript library that would do the job? Session.Input works but is somehow limited and some users complained the dialog window is eventually no longer visible on the screen.
Title: Re: EA16 JavaScript DLGInputBox bug
Post by: Sunshine on June 15, 2022, 06:12:16 pm
Just tried the code below  for fun on V16 B1605 on 64-Bit version unfortunately get the same error.

...

EAScriptLib.JavaScript-Dialog error: Exception in COMObject::Create
Classe non enregistrée, Line:26


Code: [Select]
!INC Local Scripts.EAConstants-JScript
!INC EAScriptLib.JavaScript-Dialog

function main()
{
var userInput = DLGInputBox("prompt","titre","default value");
Session.Output(userInput);
}

main();
...
Title: Re: EA16 JavaScript DLGInputBox bug
Post by: roybal on August 02, 2022, 09:17:51 am
Hello all,
Could this issue be that the current EA configuration is calling a 32-bit "ActiveX" object which is why the 64-bit version comes back with an error?

I am not sure but it sort of looks like it.

Thanks, Adam
Title: Re: EA16 JavaScript DLGInputBox bug
Post by: timoc on August 02, 2022, 08:38:09 pm
Hello all,
Could this issue be that the current EA configuration is calling a 32-bit "ActiveX" object which is why the 64-bit version comes back with an error?

I am not sure but it sort of looks like it.

Thanks, Adam

I was having similar issues, and found that if you look at the code in the include file you will see:
Code: [Select]
function DLGInputBox( promptText /* : String */, title /* : String */, defaultText /* : String */ ) /* : String */
{
// JavaScript has no intrinsic InputBox method, therefore we have to steal VBs
var vbe = new COMObject("ScriptControl");
vbe.Language = "VBScript";

return vbe.eval( "InputBox(\"" + promptText + "\",\"" + title + "\",\"" + defaultText + "\")");
}

After some experimentation with the Javascript interpreters COMObject( ) method, i started looking at the Javascript EA-Addin (https://community.sparxsystems.com/white-papers/1277-enterprise-architect-15-model-based-javascript-addin) functionality.

The JS-Addin feature claims to have EA UI integration for menu items, dialog boxes and such, but I have not yet been able to get it working in EA 16. note the EA16 plugin examples in the example repository seem to be broken (reported).

If you do manage to get the add-in route working, please post..

Update:
After some digging in the JS-Addin documentation, the dialog box i can find is the JavaScript implementation of Session.Input (https://www.sparxsystems.com/enterprise_architect_user_guide/16.0/add-ins___scripting/session_object.html).
Try this in the JavaScript console:
Code: [Select]
Session.Input("Hello")
It may be that any JS-Addin with a UI more complex than a simple dialog-box, requires a diagram with elements and uses the element properties for inputs.





Title: Re: EA16 JavaScript DLGInputBox bug
Post by: roybal on August 23, 2022, 01:09:20 am
I filed a bug report with Sparx EA about this issue.  Sparx Support confirmed my suspicion that v16 x64 is unable to call the 32-bit COM objects like "Dialog".   

This is disappointing.

From: Sparx Support <[email protected]>
Sent: Thursday, August 18, 2022 8:14 PM
Subject: Re: Registered Bug Report - Sparx EA v16 - JavaScript or JScript

Hello Adam,

Thank you for your email.
 
 Unfortunately those scripts are dependant upon 32-bit COM objects and will not work under the 64-bit version of Enterprise Architect. If you need the ability to use these script libraries then you should install the 32-bit version of EA instead.
 
 We don't have any alternative function for providing an InputBox under 64-bit at this time.
 
 Best regards,
....
 Sparx Systems Pty Ltd
 
 
Title: Re: EA16 JavaScript DLGInputBox bug
Post by: roybal on September 14, 2022, 09:22:24 am
FYI, I have installed the x86/32-bit version of EA v16.   And, the JavaScript COMObject calls work now.

One caution that I believe was mentioned before:

The EAScriptLib JavaScript-Dialog script has a bug at line-29.

The eval command needs to be Eval with a Capital E.
Title: Re: EA16 JavaScript DLGInputBox bug
Post by: Guillaume on September 23, 2022, 06:10:33 pm
Thank you for the update.
I confirm that it works in EA16 32 bits with the vbe.Eval fix copied locally. Hopefully this code will be fixed in the next EA16 build.

For the 64 bits version I wonder if there is going to be an alternative to open dialog windows and others using Javascript. Otherwise, JScript will remain the valid alternative before this is addressed.


Title: Re: EA16 JavaScript DLGInputBox bug
Post by: Geert Bellekens on September 23, 2022, 06:38:01 pm
Otherwise, JScript will remain the valid alternative before this is addressed.

Or good old VBScript :)

Geert
Title: Re: EA16 JavaScript DLGInputBox bug
Post by: PeteC on September 27, 2022, 05:31:31 pm
Otherwise, JScript will remain the valid alternative before this is addressed.

How is Jscript better? I have a script in Jscript and have come across the exact same problem (importing the JScript-Dialog fails with an almost identical error: EAScriptLib.JScript-Dialog error: Automation server can't create object, Line:26).

Is there an alternative method in Jscript that I am not aware of that will provide equivalent input? Otherwise I'll have to fallback to the Session.Input option.
Title: Re: EA16 JavaScript DLGInputBox bug
Post by: Guillaume on September 27, 2022, 06:54:30 pm
My bad, I assumed it was fine with JScript.
Even though I've extensively used VB, I tend to move to JScript or Javascript as I find the code easier to maintain. Clients also prefer to use these languages.
Title: Re: EA16 JavaScript DLGInputBox bug
Post by: JoNijsen on November 08, 2022, 09:16:06 pm
Found this one https://tablacus.github.io/scriptcontrol_en.html
in https://stackoverflow.com/questions/15549308/microsoft-script-control-64-bit
It works in EA 16.1605 64 bits,

Only modification required in EAScriptLib.JavaScript-Dialog DLGInputBox function is Eval with a capitel E
Title: Re: EA16 JavaScript DLGInputBox bug
Post by: Geert Bellekens on November 08, 2022, 09:46:44 pm
I'm using that library in my EA-Matic addin (64 bit) as well.

Geert
Title: Re: EA16 JavaScript DLGInputBox bug
Post by: Jan van Duuren on December 21, 2023, 05:28:44 pm
I did find a solution for a messagebox and inputbox using EA 16.x (64 bit) and Javascript.

for a MessageBox it is rather simple solution. Instead of using Scriptcontrol you can use WScript.Shell:

function MsgBox(prompt="", title="MsgBox", style=0) {
   style += 4096; // to make the popup active (foreground)
   var WSH = new COMObject("WScript.Shell");
   return WSH.Popup(prompt, 0, title, style);
}


Regarding an inputbox it is a little more complex. I am using two steps. First it creates a small vbs script in %TEMP% folder. Then it uses this vbs script to display the input box.

function InputBox(prompt="Input", title="Inputbox", defaultText="") {
   const fso = new COMObject("Scripting.FileSystemObject");
   const WSH = new COMObject("WScript.Shell");
   const vbsScriptFile = WSH.ExpandEnvironmentStrings("%TEMP%\\ea_inputbox.vbs");
   const tmpInputFile  = WSH.ExpandEnvironmentStrings("%TEMP%\\ea_input.tmp");
   
   if (!fso.FileExists(vbsScriptFile)) {
      CreateInputboxVBS(vbsScriptFile);
   }
   
   WSH.Run(`WScript "${vbsScriptFile}" "${tmpInputFile}" "${prompt}" "${title}" "${defaultText}"`, 1, true);
   const ForReading = 1;
   const file = fso.OpenTextFile(tmpInputFile, ForReading);
   if (!file.AtEndOfStream) {
      return file.ReadAll();
   }
}

function CreateInputboxVBS(vbsScriptFile) {
   // function to create the inputbox VBS sripts in folder
   const vbsScriptContent = `dim tmpInputFile, prompt, title, defaultText, input
      Set objArgs = WScript.Arguments
      If objArgs.Count = 4 Then
         tmpInputFile = objArgs(0)
         prompt       = objArgs(1)
         title        = objArgs(2)
         defaultText  = objArgs(3)
         input        = InputBox(prompt, title, defaultText)
      End if
         
      Const ForWriting = 2, CreateIfNeeded = true
      set fso = CreateObject("Scripting.FileSystemObject")
      set file = fso.OpenTextFile(tmpInputFile, ForWriting, CreateIfNeeded)
      file.write input
      file.close`.replace(/\t\t/g, '');
   
   const overwrite=true, unicode=true;
   const file = fso.CreateTextFile(vbsScriptFile, overwrite, unicode);
   file.Write(vbsScriptContent);
   file.Close();
}
Title: Re: EA16 JavaScript DLGInputBox bug
Post by: Guillaume on December 22, 2023, 08:32:25 pm
Hi Jan,

Thank you for the information.
I confirm that MessageBox works, however I didn't manage to get the InputBox to work (EA 16.1 build 1628 64 bits).

I copied your code to a new Javascript EA script and added the following:

   MsgBox("test");
   var testVal = InputBox("test input");
   Session.Output(testVal);

I'm getting an error "fso is not defined" related with the following line:
const file = fso.CreateTextFile(vbsScriptFile, overwrite, unicode);

Any idea ?
Title: Re: EA16 JavaScript DLGInputBox bug
Post by: Geert Bellekens on December 22, 2023, 09:11:22 pm
You might want to try to add fso to the list of dimmed variables.

Geert
Title: Re: EA16 JavaScript DLGInputBox bug
Post by: Jan van Duuren on December 22, 2023, 09:36:15 pm
yes, I have a global variable for fso.
You just can add the following line to function "CreateInputboxVBS(vbsScriptFile)"

 const fso = new COMObject("Scripting.FileSystemObject");

Code: [Select]
function InputBox(prompt="Input", title="Inputbox", defaultText="") {
   const fso = new COMObject("Scripting.FileSystemObject");
   const WSH = new COMObject("WScript.Shell");
   const vbsScriptFile = WSH.ExpandEnvironmentStrings("%TEMP%\\ea_inputbox.vbs");
   const tmpInputFile  = WSH.ExpandEnvironmentStrings("%TEMP%\\ea_input.tmp");
   
   if (!fso.FileExists(vbsScriptFile)) {
      CreateInputboxVBS(vbsScriptFile);
   }
   
   WSH.Run(`WScript "${vbsScriptFile}" "${tmpInputFile}" "${prompt}" "${title}" "${defaultText}"`, 1, true);
   const ForReading = 1;
   const file = fso.OpenTextFile(tmpInputFile, ForReading);
   if (!file.AtEndOfStream) {
      return file.ReadAll();
   }
}

function CreateInputboxVBS(vbsScriptFile) {
   // function to create the inputbox VBS sripts in folder
   const fso = new COMObject("Scripting.FileSystemObject");
   const vbsScriptContent = `dim tmpInputFile, prompt, title, defaultText, input
      Set objArgs = WScript.Arguments
      If objArgs.Count = 4 Then
         tmpInputFile = objArgs(0)
         prompt       = objArgs(1)
         title        = objArgs(2)
         defaultText  = objArgs(3)
         input        = InputBox(prompt, title, defaultText)
      End if
         
      Const ForWriting = 2, CreateIfNeeded = true
      set fso = CreateObject("Scripting.FileSystemObject")
      set file = fso.OpenTextFile(tmpInputFile, ForWriting, CreateIfNeeded)
      file.write input
      file.close`.replace(/\t\t/g, '');
   
   const overwrite=true, unicode=true;
   const file = fso.CreateTextFile(vbsScriptFile, overwrite, unicode);
   file.Write(vbsScriptContent);
   file.Close();
}
Title: Re: EA16 JavaScript DLGInputBox bug
Post by: Jan van Duuren on December 22, 2023, 10:37:37 pm
herewith a working script

Code: [Select]
!INC Local Scripts.EAConstants-JScript

/*
 * Script Name:
 * Author:
 * Purpose:
 * Date:
 */
 
function main()
{
var testVal = InputBox("enter your input here", "My InputBox");
MsgBox(testVal);
}

main();

function MsgBox(prompt="", title="MsgBox", style=0) {
style += 4096; // to make the popup active (foreground)
var WSH = new COMObject("WScript.Shell");
return WSH.Popup(prompt, 0, title, style);
}

function InputBox(prompt="Input", title="Inputbox", defaultText="") {
const fso = new COMObject("Scripting.FileSystemObject");
const WSH = new COMObject("WScript.Shell");
const vbsScriptFile = WSH.ExpandEnvironmentStrings("%TEMP%\\ea_inputbox.vbs");
const tmpInputFile  = WSH.ExpandEnvironmentStrings("%TEMP%\\ea_input.tmp");

if (!fso.FileExists(vbsScriptFile)) {
CreateInputboxVBS(vbsScriptFile);
}

WSH.Run(`WScript "${vbsScriptFile}" "${tmpInputFile}" "${prompt}" "${title}" "${defaultText}"`, 1, true);
const ForReading = 1;
const file = fso.OpenTextFile(tmpInputFile, ForReading);
if (!file.AtEndOfStream) {
return file.ReadAll();
}
return "";
}

function CreateInputboxVBS(vbsScriptFile) {
// function to create the inputbox VBS sripts in folder
const fso = new COMObject("Scripting.FileSystemObject");
const vbsScriptContent = `dim tmpInputFile, prompt, title, defaultText, input
Set objArgs = WScript.Arguments
If objArgs.Count = 4 Then
tmpInputFile = objArgs(0)
prompt       = objArgs(1)
title        = objArgs(2)
defaultText  = objArgs(3)
input        = InputBox(prompt, title, defaultText)
End if

Const ForWriting = 2, CreateIfNeeded = true
set fso = CreateObject("Scripting.FileSystemObject")
set file = fso.OpenTextFile(tmpInputFile, ForWriting, CreateIfNeeded)
file.write input
file.close`.replace(/\t\t/g, '');

const overwrite=true, unicode=true;
const file = fso.CreateTextFile(vbsScriptFile, overwrite, unicode);
file.Write(vbsScriptContent);
file.Close();
}
Title: Re: EA16 JavaScript DLGInputBox bug
Post by: Guillaume on December 23, 2023, 12:45:39 am
Hi Jan,

Thank you for the updated script, it worked  :D