Page 1 of 1

Call .vbs File from Javascript Opaque Action

PostPosted: Wed Mar 22, 2017 12:35 pm
by phathom.a.donald@lmco.com
Is it possible to call a .vbs file from a opaque action written in Javascript, or in any of the languages available to opaque actions/behaviors?

Re: Call .vbs File from Javascript Opaque Action

PostPosted: Fri Mar 24, 2017 12:58 am
by chanon_s
Dear Phathom

Please try to set the language of the opaque action to BeanShell and use the following script expressions:

Code: Select all
String[] cmd = {"cscript", "fullpath_to_your_vbs_file"};
Runtime.getRuntime().exec(cmd);


Where fullpath_to_your_vbs_file is the full path to your vbs file, for example, D:/test.vbs

Sincerely

Chanon S.
No Magic Customer Support

Re: Call .vbs File from Javascript Opaque Action

PostPosted: Mon Mar 27, 2017 6:24 am
by phathom.a.donald@lmco.com
Thank you for your response. I entered the code you suggested and got the following error message:

2017-03-27 06:25:26,252 ERROR: Cannot evaluate Opaque Action IncrementTime: bsh.EvalError: Sourced file: inline evaluation of: ``String[] cmd = {"cscript", "C:\Users\e304\Documents\Rex\IncrementTime. . . . '' Token Parsing Error: Lexical error at line 1, column 32. Encountered: "U" (85), after : "\"C:\\": <at unknown location> in <unknown file> expression body: String[] cmd = {"cscript", "C:\Users\e304\Documents\Rex\IncrementTime.vbs"}; Runtime.getRuntime().exec(cmd);

Re: Call .vbs File from Javascript Opaque Action

PostPosted: Mon Mar 27, 2017 9:54 pm
by chanon_s
Dear Phathom

Please check the path file separator. If you are going to use a slash (/), as demonstrated in our script example, it is okay to use a single one. However, if you are going to use a backslash (\), you need to use the double-backslash in your file path, as displayed below:

Code: Select all
String[] cmd = {"cscript", "C:\\Users\\e304\\Documents\\Rex\\IncrementTime.vbs"};
Runtime.getRuntime().exec(cmd);


Sincerely

Chanon S.
No Magic Customer Support

Re: Call .vbs File from Javascript Opaque Action

PostPosted: Tue Mar 28, 2017 7:41 am
by phathom.a.donald@lmco.com
That removed the error, thank you!

Now the activity diagram execution terminates abruptly after the opaque action is finished executing. Is it possible to link values on the input/output pins of the opaque action to variables in the .vbs file? I have variables declared in the .vbs file with the same names as the pins and my output variable has the same name as my file and function. See below for the code I used.

Code: Select all
String[] cmd = {"cscript", "C:\\Users\\e304\\Documents\\Rex\\NewCurrentTime.vbs", CurrentTime, String.valueOf(TimeStep)};
Runtime.getRuntime().exec(cmd);

Re: Call .vbs File from Javascript Opaque Action

PostPosted: Wed Mar 29, 2017 3:15 am
by chanon_s
Dear Phathom

Could you please attach the sample project you are using, including the IncrementTime.vbs file?

Please be informed that we are not Vbscript experts. From what we have done researching, we have not found any script engine that is directly able to support running vbscripts. The solution previously given was indirect, which was found on the Internet. So, it would not be able to directly access/return any values from/to the input/output pins, respectively.

We found a solution to pass arguments when running a vbscript via the cscript command. You can access arguments with the following syntax:

Code: Select all
WScript.Arguments(0)
'WScript.Arguments(index), where index is zero based.
'Also you can check the number of the arguments passed in your script by using WScript.Arguments.Count


However, we have not yet found any solution to return the value from the vbscript calling.

Sincerely

Chanon S.
No Magic Customer Support