11 Replies Latest reply on Nov 8, 2015 1:17 PM by Justin Putney

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

    Justin Putney Level 2

      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?

        • 1. Re: How do you run VB from JSX and return a value?
          Harbs. Level 6

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

           

          Did you try GetObject()?

           

          Harbs

          • 2. Re: How do you run VB from JSX and return a value?
            Harbs. Level 6

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

            • 3. Re: How do you run VB from JSX and return a value?
              Justin Putney Level 2

              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.

              • 4. Re: How do you run VB from JSX and return a value?
                Justin Putney Level 2

                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.

                • 5. Re: How do you run VB from JSX and return a value?
                  Justin Putney Level 2

                  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.

                  • 6. Re: How do you run VB from JSX and return a value?
                    Harbs. Level 6

                    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);
                    
                    • 7. Re: How do you run VB from JSX and return a value?
                      Vamitul Level 4

                      InDesign.Application.CS6

                      InDesign.Application.CC

                      InDesign.Application.CC.2014

                      InDesign.Application.CC.2015

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

                      • 8. Re: How do you run VB from JSX and return a value?
                        Justin Putney Level 2

                        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.

                        • 9. Re: How do you run VB from JSX and return a value?
                          Justin Putney Level 2

                          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.

                          • 10. Re: How do you run VB from JSX and return a value?
                            Harbs. Level 6

                            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...

                            • 11. Re: How do you run VB from JSX and return a value?
                              Justin Putney Level 2

                              I can imagine.

                               

                              Thanks for sharing it!