Reputation: 264
I'm running into an issue with sending data from JSX to JS using evalScript in Photoshop.
In the following example I attempt to send the document width, height and ruler units within an object, docInfo, back to my CEP Panel JS.
The issue is that when I parse the object it shows the three properties all as objects, when I believe they should all be strings.
JS:
function psdInfo() {
csInterface.evalScript("getDocInfo()", function(res) {
if (EvalScript_ErrMessage == res) {
console.error('EvalScript_ErrMessage and res is' + typeof res + ' res is ' + res);
}
else {
var o = JSON.parse(res);
var str = "Object received from JSX:\n";
for (var prop in o) {
str += prop + " [" + typeof o[prop] +" ]: " + o[prop] + ".\n";
}
console.log(str);
}
});
}
JSX:
function getDocInfo()
{
var doc = app.activeDocument,
docWidth = doc.width,
docHeight = doc.height,
originalRulerUnits = app.preferences.rulerUnit;
var docInfo = {
width: docWidth,
height: docHeight,
originalRulerUnits: originalRulerUnits
};
return JSON.stringify(docInfo);
}
What I'd expect:
Object received from JSX:
width [string ]: '1920 px'.
height [string ]: '1920 px'.
originalRulerUnits [string ]: 'UNITS.PIXELS'.
What I get:
Object received from JSX:
width [object ]: [object Object].
height [object ]: [object Object].
originalRulerUnits [object ]: [object Object].
My question is - am I doing something wrong? Are these in fact objects?
If they are, how do I get the info I'm after? How do I parse/convert the object on the JSX side to string or should I do that on the JS side?
An example of how-to would be really helpful. I'm trying to learn Adobe CEP panels.
Upvotes: 2
Views: 669
Reputation: 2269
Those are specific to Photoshop objects defined by ExtendScript: height and width are UnitValues
(they carry both a value and a unit) and rulerUnits are Units
, JSON can't correctly pass them as objects from JSX to JS. So before passing docInfo
you need to make sure you've converted them to strings using .toString()
method:
function getDocInfo()
{
var doc = app.activeDocument,
docWidth = doc.width.toString(),
docHeight = doc.height.toString(),
originalRulerUnits = app.preferences.rulerUnits.toString();
var docInfo = {
width: docWidth,
height: docHeight,
originalRulerUnits: originalRulerUnits
};
return JSON.stringify(docInfo);
}
result:
{"width":"1024 px","height":"1024 px","originalRulerUnits":"Units.PIXELS"}
You can read more about specific object in JavaScript Tools Guide pdf: there're File
, Folder
, SolidColor
and others.
Upvotes: 1