Copy link to clipboard
Copied
I'm writing a script to pull some attributes out of existing Photoshop files.
I'm currently trying find a way to get the corner radius of an existing shape layer (vector). Does anyone know if that's possible and if so how to do it?
Thanks
Coming back to my own question after a couple years:
Turns out, each layer in PS has a 'json' property you can get from a bit of ActionManager code.
(I learned how to write this from jazz-y. Thanks!):
var r = new ActionReference();
r.putProperty(charIDToTypeID('Prpr'), stringIDToTypeID('json'));
r.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
var thisJson = executeActionGet(r).getString(stringIDToTypeID('json'));
alert(thisJson);
The JSON st
...
eval('(' + thisJson + ')').layers[0].path.pathComponents[0].origin.radii
Copy link to clipboard
Copied
Coming back to my own question after a couple years:
Turns out, each layer in PS has a 'json' property you can get from a bit of ActionManager code.
(I learned how to write this from jazz-y. Thanks!):
var r = new ActionReference();
r.putProperty(charIDToTypeID('Prpr'), stringIDToTypeID('json'));
r.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
var thisJson = executeActionGet(r).getString(stringIDToTypeID('json'));
alert(thisJson);
The JSON string returned has a wealth of info about a layer (depending on the type of layer). In the case of a rounded rectangle it returns this lovely string:
{
"version": "1.6.1",
"timeStamp": 1609818097.180,
"count": 138,
"id": 897,
"file": "\/Users\/steve\/Dropbox\/_ToolsLocal\/_ScriptsLocal\/Photoshop\/Shapes\/ColorStyleProcessing\/textSample.psd",
"bounds": {
"top": 0,
"left": 0,
"bottom": 768,
"right": 1024
},
"selection": [1],
"resolution": 72,
"globalLight": {
"angle": 90,
"altitude": 30
},
"generatorSettings": false,
"profile": "Display P3",
"mode": "RGBColor",
"depth": 8,
"layers": [{
"id": 122,
"index": 1,
"type": "shapeLayer",
"name": "Rounded Rectangle 1",
"bounds": {
"top": 161,
"left": 467,
"bottom": 348,
"right": 594
},
"visible": true,
"clipped": false,
"fill": {
"color": {
"red": 203.626,
"green": 38.0039,
"blue": 213.988
},
"class": "solidColorLayer"
},
"strokeStyle": {
"strokeStyleVersion": 2,
"strokeEnabled": false,
"fillEnabled": true,
"strokeStyleLineWidth": 2,
"strokeStyleLineDashOffset": {
"value": 0,
"units": "pointsUnit"
},
"strokeStyleMiterLimit": 100,
"strokeStyleLineCapType": "strokeStyleButtCap",
"strokeStyleLineJoinType": "strokeStyleMiterJoin",
"strokeStyleLineAlignment": "strokeStyleAlignCenter",
"strokeStyleScaleLock": false,
"strokeStyleStrokeAdjust": false,
"strokeStyleBlendMode": "normal",
"strokeStyleOpacity": {
"value": 100,
"units": "percentUnit"
},
"strokeStyleContent": {
"color": {
"red": 247,
"green": 225.689,
"blue": 225.689
}
},
"strokeStyleResolution": 72
},
"generatorSettings": false,
"path": {
"pathComponents": [{
"origin": {
"radii": [2, 2, 40, 40],
"type": "roundedRect",
"bounds": {
"top": 161,
"left": 467,
"bottom": 348,
"right": 594
}
}
}],
"bounds": {
"top": 161,
"left": 467,
"bottom": 348,
"right": 594
},
"defaultFill": false
}
}]
}
You'll notice that under 'pathComponents', there's something called 'radii'. (Which as far as I know, is only a property used for rounded rectangles). The following numbers in square brackets are your corner radii:
"radii": [2, 2, 40, 40],
Parsing JSON in ExtendScript (basically a 20-year old version of Javascript) isn't a good time, but if you're just trying to get one specific thing it's not so bad. You can use splitting:
// split the string in halves at 'radii' and grab the second half:
var myRads = thisJson.split("\"radii\":\[")[1];
// then split again, grabbing the first segment before the ']':
myRads = myRads.split("\]")[0]; //returns '2, 2, 40, 40';
// one more split gives you an array of the 4 numbers:
myRads = myRads.split(',');
// See results:
var str = "topRight: "+myRads[0]+"\n"+
"bottomRight: "+myRads[1]+"\n"+
"bottomLeft: "+myRads[2]+"\n"+
"topLeft: "+myRads[3];
alert(str);
/* Results:
topRight: 2
bottomRight: 2
bottomLeft: 40
topLeft: 40
*/
Copy link to clipboard
Copied
eval('(' + thisJson + ')').layers[0].path.pathComponents[0].origin.radii
Copy link to clipboard
Copied
Wow. Kukurykus, it would be awesome to treat the json property as an object, but with that code I get this:
Error 21: undefined is not an object.
-> var str = thisJson.layers[0].path.pathComponents[0].origin.radii;
How are you doing that? As far as I know JSONparse is not available in ExtendScript?
Copy link to clipboard
Copied
Ah. I see 'eval' now. Thanks!
Copy link to clipboard
Copied
You are right, in this old implementation evaluation was still used, that now is even not deprecated but completely replaced by JSON.parse().