3 Replies Latest reply on Jan 11, 2017 12:05 AM by Harbs.

    How to pass information between JS Extension and JSX host

    Jens Troeger Level 1

      Hello,

       

      My CEP extension for InDesign CC runs some HTML/Javascript code which fetches data from a remote REST API host. It then executes ExtendScript code using the CSInterface:

       

      $.get({
          url: "some.url",
      }).done(function (data, textStatus, jqXHR) {
          var csInterface = new CSInterface();
          csInterface.evalScript("../jsx/myscript.jsx"); 
      }).fail(...);
      

       

      How can I pass Javascript’s data to the ExtendScript script?

      • It doesn’t seem like localStorage is available across boundaries between the host application’s core (InDesign) and CEP extension runtime. What about sessionStorage?
      • I’d like to avoid using files on the host (Javascript FileSaver, and ExtendScript File) unless there is a way to use proper temporary files in the system’s temp folder.

       

      Cheers!

        • 1. Re: How to pass information between JS Extension and JSX host
          Harbs. Level 6

          You should use evalScript with a function rather than eval'ing a whole file.

           

          You can pass a string into the eval'ed function and JSON.stringify() and JSON.parse() on both ends is your friend. You'll need to include JSON2 in your ExtendScript for that to work: GitHub - douglascrockford/JSON-js: JSON in JavaScript

           

          HTH,

          Harbs

          • 2. Re: How to pass information between JS Extension and JSX host
            Jens Troeger Level 1

            Thank you, Harbs. but the following code did not work, if that is what you propose:

             

            var csInterface = new CSInterface();
            csInterface.evalScript(function (data) {
                alert("123");
                var myDocument = app.documents.add();
                var myPage = myDocument.pages.item(0);
                var myTextFrame = myPage.textFrames.add();
                myTextFrame.contents = "This is some example text.";
            });
            

             

            As per its source code here, the evalScript() function takes a script as its first argument but doesn’t seem to allow a plain function. (Which makes sense considering that a function and its closure would have to be passed across two script engines for execution; whereas passing a string containing the script is trivial to pass across.) The example code here illustrates this.

            • 3. Re: How to pass information between JS Extension and JSX host
              Harbs. Level 6

              You did not understand me. Let me explain better.

               

              In jsx:

              function doSomethingWithSomeData(objStr){
                  var obj = JSON.parse(objStr);
                  // do something with the obj
              }
              

              In CEP:

              var csInterface = new CSInterface();  
              csInterface.evalScript("doSomethingWithSomeData('"+JSON.stringify(myData)+"')");  
              

              Make sure your jsx is evaluated when your panel first loads, so the function is available to the scripting engine.

               

              If you need to do something with the result, pass a function to the second argument of runScript which will be executed async with the results of the jsx. In that case, you'd usually use JSON.stringify() in the jsx and JSON.parse() in CEP.

               

              HTH,

              Harbs