Highlighted

Jsx working with object

Participant ,
Mar 02, 2019

Copy link to clipboard

Copied

Accordinf what ive read,  If you need to pass complex parameters such as objects from your panel’s JS to the host app's JSX, JSON.stringify() them first (no need to JSON.parse() them in the JSX – they arrive as objects).

in in jsx I have the following object.

myData

this was passed from js to jsx via evalscript(myData).

i can view it and see there’s an object with the data.

My problem is I need to run a few things on this object in example: Object.keys(myData).length should return the length but instead it does nothing. 

Am I missing something? 

Topics

Exchange

Views

2.2K

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Jsx working with object

Participant ,
Mar 02, 2019

Copy link to clipboard

Copied

Accordinf what ive read,  If you need to pass complex parameters such as objects from your panel’s JS to the host app's JSX, JSON.stringify() them first (no need to JSON.parse() them in the JSX – they arrive as objects).

in in jsx I have the following object.

myData

this was passed from js to jsx via evalscript(myData).

i can view it and see there’s an object with the data.

My problem is I need to run a few things on this object in example: Object.keys(myData).length should return the length but instead it does nothing. 

Am I missing something? 

Topics

Exchange

Views

2.2K

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Valorous Hero ,
Mar 04, 2019

Copy link to clipboard

Copied

Hi,

If you stringify the object in the JS layer, then it comes as string in the JSX environment. You then need to "eval" that string as an object but JSON.parse isn't available by default. So you need to use Douglas Crockford's JSON2 library : JSON-js/json2.js at master · douglascrockford/JSON-js · GitHub

Just for you know, Marc Autret​ proved this lib to have some non ExtendScript Compliant issues so you may give his implementation a try : GitHub - indiscripts/IdExtenso: ExtendScript Framework for InDesign Ninjas

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Participant ,
Mar 04, 2019

Copy link to clipboard

Copied

Elementary question but even this has scarce info, how do I include the JSON-js script in my jsx?

I tried this:

#include (..js/json.js);

@include (..js/json.js);

Then in json js I add a test function

testMyScript() {

     alert('hello');

};

Attempting to run it from jsx doesn't work.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Engaged ,
Mar 04, 2019

Copy link to clipboard

Copied

$.evalFile(pathToJSON)

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Participant ,
Mar 04, 2019

Copy link to clipboard

Copied

I have two files.

JS -> JSX

js contains:

var myDataHere = JSON.stringify(jsObjVariable);

I pass it to jsx like this:

csi.evalScript("myJSXfunction+'+(myDataHere)+'+");

in my JSX file I have

function myJSXfunction(dataToUse);

{

// according to your example would I do this?

var newParsedData = $.evalFile(dataToUse)

}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Engaged ,
Mar 06, 2019

Copy link to clipboard

Copied

$.evalFile(pathToJSON) is a way to include the json2.js:

js file:

var csInterface = new CSInterface();

//get the json2.js path dynamically (I assumed here the file is in your extension folder in the "js" folder)

var pathToJSONSource = csInterface.getSystemPath(SystemPath.EXTENSION) + "/js/json2.js";

//call the function "myJSXFunction" in the jsx file and pass the path of the json2 and your json data as string

csInterface.evalScript("myJSXFunction('" + pathToJSONSource + "','" + JSON.stringify(myJSONData) + "');"

jsx file:

function myJSXFunction(pathToJSONSource, myJSONData) {

     $.evalFile(pathToJSONSource);

     var jsonObj = JSON.parse(myJSONData);

}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Explorer ,
Oct 22, 2019

Copy link to clipboard

Copied

Hi @tomzag

Please suggest me to return json string from jsx and parse it in js in photoshop.

Including the json library Json2.js using #include make jsx not not to work.

Suggest me some other way.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...