Copy link to clipboard
Copied
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?
Cheers!
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
...Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Thanks you made my day.