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

Jsx working with object

Engaged ,
Mar 02, 2019 Mar 02, 2019

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

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

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

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.

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

$.evalFile(pathToJSON)

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

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)

}

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
Engaged ,
Mar 06, 2019 Mar 06, 2019

$.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);

}

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 ,
Oct 22, 2019 Oct 22, 2019
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.

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