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

How to pass information between JS Extension and JSX host

Explorer ,
Jan 10, 2017 Jan 10, 2017

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!

TOPICS
Scripting
5.3K
Translate
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 , Jan 11, 2017 Jan 11, 2017

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

...
Translate
LEGEND ,
Jan 10, 2017 Jan 10, 2017

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

Translate
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
Explorer ,
Jan 10, 2017 Jan 10, 2017

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.

Translate
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 ,
Jan 11, 2017 Jan 11, 2017

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

Translate
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 ,
Aug 17, 2017 Aug 17, 2017
LATEST

Thanks you made my day.

Translate
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