Copy link to clipboard
Copied
Hi All,
Is it possible to get the nested smart object layer names in the psd file using script?
Copy link to clipboard
Copied
There is useful information here
Copy link to clipboard
Copied
Thanks r-bin!
I have found some information from the above thread. From the thread, AM code get the smart object layer name from the first level of smart object but in my psd files contains more than 5 nested of smart objects. Is it possible to get the nested smart object layer names?
Please find the attached psd file for reference, in the psd file contains with test1, test2, test3 nested smart object layers, my requirement is to get three smart object layer names & final image layer name
Copy link to clipboard
Copied
It was a hint.
All information is in the json object.
You just need to extract it in the form you need.
For example
//////////////////////////////////////////////
function show_tree(json)
{
try {
var s = "";
show_layers(json.placed, json.layers, "");
alert(s);
function show_layers(placed, layers, level)
{
try {
if (layers)
{
for (var i = 0; i < layers.length; i++)
{
var prefix = "Layer: ";
if (layers[i].layers) prefix = "Group: ";
if (layers[i].smartObject) prefix = "SmartObject: ";
s += level + prefix + layers[i].name + "\n";
if (layers[i].smartObject) show_layers(placed, get_placed(placed, layers[i].smartObject.ID), "- " + level);
if (layers[i].layers) show_layers(placed, layers[i].layers, "- " + level);
}
}
}
catch (e) { throw(e); }
}
function get_placed(placed, placed_id)
{
try {
if (placed)
{
for (var i = 0; i < placed.length; i++)
{
if (placed[i].placedID == placed_id) return placed[i].layers;
if (placed[i].placed) return get_placed(placed[i].placed, placed_id);
}
}
return null;
}
catch (e) { throw(e); }
}
}
catch (e) { alert(e); }
}
//////////////////////////////////////////////
try {
var r = new ActionReference();
var d = new ActionDescriptor();
r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("json"));
r.putEnumerated(stringIDToTypeID("document"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
d.putReference(stringIDToTypeID("null"), r);
d.putBoolean(stringIDToTypeID("expandSmartObjects"), true);
eval("var json="+executeAction(stringIDToTypeID("get"), d, DialogModes.NO).getString(stringIDToTypeID("json")));
show_tree(json);
alert(json.toSource());
}
catch (e) { alert(e); }
Copy link to clipboard
Copied
A error in the algorithm in the get_placed() function.
New code
/////////////////////////////////////////////////////////
function show_tree(json)
{
try {
var s = "";
show_layers(json.placed, json.layers, "");
alert(s);
function show_layers(placed, layers, level)
{
try {
if (layers)
{
for (var i = 0; i < layers.length; i++)
{
var prefix = "Layer: ";
if (layers[i].layers) prefix = "Group: ";
if (layers[i].smartObject) prefix = "SmartObject: ";
s += level + prefix + layers[i].name + "\n";
if (layers[i].smartObject) show_layers(placed, get_placed(placed, layers[i].smartObject.ID), "- " + level);
if (layers[i].layers) show_layers(placed, layers[i].layers, "- " + level);
}
}
}
catch (e) { throw(e); }
}
function get_placed(placed, placed_id)
{
try {
if (placed)
{
for (var i = 0; i < placed.length; i++)
{
if (placed[i].placedID == placed_id) return placed[i].layers;
if (placed[i].placed)
{
var p = get_placed(placed[i].placed, placed_id);
if (p) return p;
}
}
}
return null;
}
catch (e) { throw(e); }
}
}
catch (e) { alert(e); }
}
/////////////////////////////////////////////////////////
try {
var r = new ActionReference();
var d = new ActionDescriptor();
r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("json"));
r.putEnumerated(stringIDToTypeID("document"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
d.putReference(stringIDToTypeID("null"), r);
d.putBoolean(stringIDToTypeID("expandSmartObjects"), true);
eval("var json="+executeAction(stringIDToTypeID("get"), d, DialogModes.NO).getString(stringIDToTypeID("json")));
show_tree(json);
alert(json.toSource());
}
catch (e) { alert(e); }
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Thanks r-bin,
Now I can able to get all the nested smart object layer names from the json data. Now, I want to generate as report excel with artboard name(from the psd file) in one column along with respective nested smart objects layer names in another column, the 3rd to 5th column is final image size, resolution and name in the last smart object
 
Final Image(to get size & resolution):