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

Jsx working with object

Contributor ,
Mar 02, 2019 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
Scripting

Views

3.1K

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
community guidelines
Adobe
People's Champ ,
Mar 04, 2019 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

Votes

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
community guidelines
Contributor ,
Mar 04, 2019 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.

Votes

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
community guidelines
Engaged ,
Mar 04, 2019 Mar 04, 2019

Copy link to clipboard

Copied

$.evalFile(pathToJSON)

Votes

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
community guidelines
Contributor ,
Mar 04, 2019 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)

}

Votes

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
community guidelines
Engaged ,
Mar 06, 2019 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);

}

Votes

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
community guidelines
Explorer ,
Oct 22, 2019 Oct 22, 2019

Copy link to clipboard

Copied

LATEST

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.

Votes

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
community guidelines