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

To get the nested smart object layer names

Contributor ,
May 16, 2022 May 16, 2022

Copy link to clipboard

Copied

Hi All,

Is it possible to get the nested smart object layer names in the psd file using script?

TOPICS
Actions and scripting , macOS

Views

390

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
Valorous Hero ,
May 17, 2022 May 17, 2022

Copy link to clipboard

Copied

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 ,
May 18, 2022 May 18, 2022

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

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
Valorous Hero ,
May 18, 2022 May 18, 2022

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);  }

 

Untitled-1.jpg

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
Valorous Hero ,
May 18, 2022 May 18, 2022

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);  }

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
Community Expert ,
May 18, 2022 May 18, 2022

Copy link to clipboard

Copied

Oops, links removed, they lead to the same place as r-bin's!

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 ,
May 23, 2022 May 23, 2022

Copy link to clipboard

Copied

LATEST

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

Screenshot 2022-05-23 at 11.05.00 PM.png

 
Final Image(to get size & resolution):
 

Screenshot 2022-05-23 at 11.33.11 PM.png

 

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