Skip to main content
Justin Putney
Known Participant
November 7, 2015
Answered

How do you run VB from JSX and return a value?

  • November 7, 2015
  • 2 replies
  • 3160 views

Anyone know how to do this?

$.writeln('result:\r' + app.doScript('wsh.echo "test"', ScriptLanguage.VISUAL_BASIC));

Both wsh and wscript give an 'Object Required' error. Same script works fine as vbs file.

It sounds like those objects just aren't available when running from doScript, but is there some other way to return a value?

This topic has been closed for replies.
Correct answer Harbs.

Harbs. wrote:

FWIW, to return a value from VB using doScript, you need returnValue = SomeValue at the end of your VB script.

I know that's the case with a Sub or a Function...is that how it works at the end of a script as well?

Harbs. wrote:

Did you try GetObject()?

Interesting. Hadn't heard of that before. I can't quite tell how it would apply from the documentation so far.


Justin Putney wrote:

Harbs. wrote:

FWIW, to return a value from VB using doScript, you need returnValue = SomeValue at the end of your VB script.

I know that's the case with a Sub or a Function...is that how it works at the end of a script as well?

To answer your questions, here's a working snippet I have which gets the current file from Excel:

var vbs = 'Set MyXL = GetObject(, "Excel.Application")\r' +

'Set ActiveXL = MyXL.activeworkbook\r'+

'returnValue = ActiveXL.Path';

var path = app.doScript(vbs,ScriptLanguage.VISUAL_BASIC);

2 replies

Justin Putney
Known Participant
November 8, 2015

It looks like this will work:

var scriptArr = [

'Set myInDesign = CreateObject("InDesign.Application")',

'myInDesign.insertLabel "myLabel", "text msg goes here"  '];

app.doScript(scriptArr.join('\r'), ScriptLanguage.VISUAL_BASIC);

$.writeln('stored value: ' + app.extractLabel('myLabel'));

//result = stored value: text msg goes here

Run the vb code, set a script label there, then retrieve it in extendscript.

My only concern with this solution is the uncertainty about which version of InDesign the vbscript is targeting.

I'm going to look through the vbscript documentation to see if there's way to target a specific version of InDesign (didn't see it on first glance), but I'd welcome suggestions.

Justin Putney
Known Participant
November 8, 2015

This seems to work just as well:

var scriptArr = [

'Set myInDesign = CreateObject("InDesign.Application")',

'myInDesign.ScriptArgs.SetValue "myLabel", "text msg goes here"  '];

app.doScript(scriptArr.join('\r'), ScriptLanguage.VISUAL_BASIC);

$.writeln('stored value: ' + app.scriptArgs.getValue('myLabel'));

Still have the issue about targeting the exact version of InDesign (in case more than one version is open). The documentation says this will target the last version ID that was launched. It seems there's a way to use InDesign.Application.CS6, but I haven't found an equivalent for CC versions.

Vamitul
Legend
November 8, 2015

InDesign.Application.CS6

InDesign.Application.CC

InDesign.Application.CC.2014

InDesign.Application.CC.2015

And 'myInDesign.ScriptArgs.SetValue' is the proper way to go.

Harbs.
Legend
November 7, 2015

I remember struggling with a similar problem a long time ago, but I don't remember details.

Did you try GetObject()?

Harbs

Harbs.
Legend
November 7, 2015

FWIW, to return a value from VB using doScript, you need returnValue = SomeValue at the end of your VB script.

Justin Putney
Known Participant
November 7, 2015

Harbs. wrote:

FWIW, to return a value from VB using doScript, you need returnValue = SomeValue at the end of your VB script.

I know that's the case with a Sub or a Function...is that how it works at the end of a script as well?

Harbs. wrote:

Did you try GetObject()?

Interesting. Hadn't heard of that before. I can't quite tell how it would apply from the documentation so far.