• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

Enthusiast ,
Nov 07, 2015 Nov 07, 2015

Copy link to clipboard

Copied

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?

TOPICS
Scripting

Views

2.2K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Nov 07, 2015 Nov 07, 2015

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.

...

Votes

Translate

Translate
LEGEND ,
Nov 07, 2015 Nov 07, 2015

Copy link to clipboard

Copied

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

Did you try GetObject()?

Harbs

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 07, 2015 Nov 07, 2015

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Nov 07, 2015 Nov 07, 2015

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 07, 2015 Nov 07, 2015

Copy link to clipboard

Copied

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);

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Nov 08, 2015 Nov 08, 2015

Copy link to clipboard

Copied

Harbs. wrote:

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:

  1. var vbs = 'Set MyXL = GetObject(, "Excel.Application")\r'
  2. 'Set ActiveXL = MyXL.activeworkbook\r'
  3. 'returnValue = ActiveXL.Path'
  4. var path = app.doScript(vbs,ScriptLanguage.VISUAL_BASIC); 

Ah, I thought returnValue was a hypothetical, but it has to be exactly "returnValue". Thanks for the example, Harbs! Is that documented anywhere? I couldn't seem to find it.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 08, 2015 Nov 08, 2015

Copy link to clipboard

Copied

Justin Putney wrote:

Ah, I thought returnValue was a hypothetical, but it has to be exactly "returnValue". Thanks for the example, Harbs! Is that documented anywhere? I couldn't seem to find it.

To tell you the truth, I do not remember where I got that from. It might have been trial and error. I do remember going crazy trying to figure it out...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Nov 08, 2015 Nov 08, 2015

Copy link to clipboard

Copied

I can imagine.

Thanks for sharing it!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 29, 2018 Mar 29, 2018

Copy link to clipboard

Copied

it has to be returnValue !

thanks for sharing.

stole me also one hour #headbanging

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 03, 2023 Jul 03, 2023

Copy link to clipboard

Copied

LATEST

Why isn't that documented?? In case someone doesn't figure out herself: Obviously returnValue needs to be declared inside the vbs, like

dim resultValue

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Nov 07, 2015 Nov 07, 2015

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Nov 07, 2015 Nov 07, 2015

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Nov 08, 2015 Nov 08, 2015

Copy link to clipboard

Copied

InDesign.Application.CS6

InDesign.Application.CC

InDesign.Application.CC.2014

InDesign.Application.CC.2015

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Nov 08, 2015 Nov 08, 2015

Copy link to clipboard

Copied

Perfect. Tested and it works on my end. Thanks, Vamitual!

Vamitul wrote:

InDesign.Application.CS6

InDesign.Application.CC

InDesign.Application.CC.2014

InDesign.Application.CC.2015

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines