7 Replies Latest reply on Feb 15, 2017 4:44 AM by Kasyan Servetsky

    VBScript to call JavaScript

    Ghoulfool Level 1

      See if you guys can help me:

       

      Is there a way for a Photoshop VBScript to call a JavaScript file?

      Or at least pass some user input (variable or return from function) from one script to another.

      Here's what I have. This simple VBScript asks for the user to type in their name which is then created as text in the second script.

      VBScript

      ' Ask User for input
      Dim
      appRef Set appRef = CreateObject( "Photoshop.Application" )
      Dim askName : askName = InputBox("Enter name: ")

      JavaScript

      // create a document to work with
      var docRef = app.documents.add(200, 100, 72, "Hello");

      // Create a new art layer containing text
      var artLayerRef
      = docRef.artLayers.add();
      artLayerRef
      .kind = LayerKind.TEXT;

      // Set the contents of the text layer.
      var textItemRef
      = artLayerRef.textItem
      textItemRef
      .contents = "Hello " + askName

      What do I need to connect the two up?

        • 1. Re: VBScript to call JavaScript
          pixxxel schubser MVP & Adobe Community Professional

          Sorry.jpg

           

          Sorry.

          You always asking but never give credits to your helper.

          • 2. Re: VBScript to call JavaScript
            Ghoulfool Level 1

            Probably because that's not been pointed out to me

            Whooops! My bad

            1 person found this helpful
            • 3. Re: VBScript to call JavaScript
              Kasyan Servetsky Level 5

              You don't need VBS for this. JS has Prompt.

              13-02-2017 13-46-51.png

              main();
              
              function main() {
                  var askName = prompt("Enter name:", "", "My script");
                  // if the user hits 'Cancet', it returns null, otherwise the value of the text edit field 
                  if (askName != null) {
                      // create a document to work with
                      var docRef = app.documents.add(200, 100, 72, "Hello");
              
                      // Create a new art layer containing text
                      var artLayerRef = docRef.artLayers.add();
                      artLayerRef.kind = LayerKind.TEXT;
              
                      // Set the contents of the text layer.
                      var textItemRef = artLayerRef.textItem
                      textItemRef.contents = "Hello " + askName;
                  }
              }
              

               

              13-02-2017 13-50-06.png

              — Kas

              1 person found this helpful
              • 4. Re: VBScript to call JavaScript
                Ghoulfool Level 1

                Kas,

                I know about JavaScript UI but... I've been having the same problems as this guy: Script won't create UI as window does not have a constructor. The problem is erratic and hard to pin down. I do NOT want to have to delete PS prefs each time.

                 

                So I was wondering if it was possible to get VBS UI to call the JavaScript. - Although I might end up doing something in HTML instead

                • 5. Re: VBScript to call JavaScript
                  Kasyan Servetsky Level 5

                  Unlike InDesign, Photoshop doesn't have the 'doScript' method so, of the top of my head,  my answer is no, it's impossible. Maybe there's a workaround but I don't know about it.

                  In my opinion, the easiest approach, in this case, would be to translate the script from JS to VBA.

                  — Kas

                  • 7. Re: VBScript to call JavaScript
                    Kasyan Servetsky Level 5

                    I looked for it only in the JS reference: obviously it wasn't there. It is in the VBS reference (last time wrote in VB for Photoshop somewhere in 2004 -- that's why I didn't look here) so theoretically it should work but, for some reason, VBS-files don't appear in my scripts menu (tested in CC 2015 and 17). Also, they're unavailable via 'Browse' menu: it allows to choose only JS.

                    CC 2015

                    15-02-2017 14-23-20.png

                     

                    CC 2017

                    15-02-2017 14-20-10.png

                    Here they give a contradictory information:

                    First they say: "In Windows, you can use scripting languages that support COM automation, such as VB Script."

                    A few lines below: "Choose File > Scripts and then select the script from the list. The scripts list includes all the script files saved with a .js or .jsx extension and saved in the Photoshop CS5/Presets/Scripts folder. To run a script saved in another location, choose File > Scripts > Browse and navigate to the script."

                     

                    — Kas