Skip to main content
New Participant
December 4, 2018
Answered

Layer group from folders or a text file

  • December 4, 2018
  • 2 replies
  • 4374 views

This should be easy but like many things, in photoshop it's not.

I would like to load a series of images, and the folder names into group layers with the images inside the groups.

It's basically loading my file folder structure into a PSD file but there is no way to do that.

You can only load images using the stacks load script.

--

Does anyone know how to load folder structures as groups and the images in those sub-folders as groups?

I know this could be a problem but it was a simple csv file, there should be a way to automate this with script.

--

Thanks for any help

Frank

This topic has been closed for replies.
Correct answer r-bin

Perhaps this is what you wanted.

var folder = null;

try {

    activeDocument;

    folder = Folder.selectDialog("Select your folder", "C:\\");

    }

catch(e)

    {

    alert("No activeDocument", "");

    }

   

if (folder)

    {

    refresh();

       

    place_files(folder);

    alert("Done!", "");    

    }       

function place_files(folder) 

    { 

    try 

        { 

        var layer = activeDocument.activeLayer;

   

        add_group(folder.name);

        add_layer(folder.name);

        layer = activeDocument.activeLayer;

        var f = folder.getFiles(); 

        for (var i = 0; i < f.length; i++) 

            { 

            if (f instanceof Folder)  

                {

                place_files(f); 

                activeDocument.activeLayer = layer;

                }

            else 

                if (f.name.match(/\.(psd|jpg|tif|png)$/i))

                    {

                    place_file(f);

                    }

            } 

        layer.remove();

        f = null; 

        } 

    catch (e) { alert(e); } 

    } 

function add_group(name)

    {

    try {

        var d = new ActionDescriptor();

        var r = new ActionReference();

        r.putClass(stringIDToTypeID("layerSection"));

        d.putReference(stringIDToTypeID("null"), r);

        var d1 = new ActionDescriptor();

        d1.putString(stringIDToTypeID("name"), name);

        d.putObject(stringIDToTypeID("using"), stringIDToTypeID("layerSection"), d1);

        executeAction(stringIDToTypeID("make"), d, DialogModes.NO);

        }

    catch (e) { alert(e); throw(e); }

    }

function add_layer(name)

    {

    try {

        var d = new ActionDescriptor();

        var r = new ActionReference();

        r.putClass(stringIDToTypeID("layer"));

        d.putReference(stringIDToTypeID("null"), r);

        var d1 = new ActionDescriptor();

        d1.putString(stringIDToTypeID("name"), name);

        d.putObject(stringIDToTypeID("using"), stringIDToTypeID("layer"), d1);

        executeAction(stringIDToTypeID("make"), d, DialogModes.NO);

        }

    catch (e) { alert(e); throw(e); }

    }

function place_file(file)

    {

    try {

        var d = new ActionDescriptor();

        d.putPath(stringIDToTypeID("file"), file);

        var doc = executeAction(stringIDToTypeID("openViewlessDocument"), d, DialogModes.NO).getData(stringIDToTypeID("document"));

        var d = new ActionDescriptor();

        var l = new ActionList();

        l.putPath(file);

        d.putList(stringIDToTypeID("fileList"), l);

        var l = new ActionList();

        l.putData(doc);

        d.putList(stringIDToTypeID("viewlessDoc"), l);

        executeAction(stringIDToTypeID("addLayerFromViewlessDoc"), d, DialogModes.NO);

        }

    catch (e) { alert(e); throw(e); }

    }

2 replies

r-binCorrect answer
Legend
February 9, 2019

Perhaps this is what you wanted.

var folder = null;

try {

    activeDocument;

    folder = Folder.selectDialog("Select your folder", "C:\\");

    }

catch(e)

    {

    alert("No activeDocument", "");

    }

   

if (folder)

    {

    refresh();

       

    place_files(folder);

    alert("Done!", "");    

    }       

function place_files(folder) 

    { 

    try 

        { 

        var layer = activeDocument.activeLayer;

   

        add_group(folder.name);

        add_layer(folder.name);

        layer = activeDocument.activeLayer;

        var f = folder.getFiles(); 

        for (var i = 0; i < f.length; i++) 

            { 

            if (f instanceof Folder)  

                {

                place_files(f); 

                activeDocument.activeLayer = layer;

                }

            else 

                if (f.name.match(/\.(psd|jpg|tif|png)$/i))

                    {

                    place_file(f);

                    }

            } 

        layer.remove();

        f = null; 

        } 

    catch (e) { alert(e); } 

    } 

function add_group(name)

    {

    try {

        var d = new ActionDescriptor();

        var r = new ActionReference();

        r.putClass(stringIDToTypeID("layerSection"));

        d.putReference(stringIDToTypeID("null"), r);

        var d1 = new ActionDescriptor();

        d1.putString(stringIDToTypeID("name"), name);

        d.putObject(stringIDToTypeID("using"), stringIDToTypeID("layerSection"), d1);

        executeAction(stringIDToTypeID("make"), d, DialogModes.NO);

        }

    catch (e) { alert(e); throw(e); }

    }

function add_layer(name)

    {

    try {

        var d = new ActionDescriptor();

        var r = new ActionReference();

        r.putClass(stringIDToTypeID("layer"));

        d.putReference(stringIDToTypeID("null"), r);

        var d1 = new ActionDescriptor();

        d1.putString(stringIDToTypeID("name"), name);

        d.putObject(stringIDToTypeID("using"), stringIDToTypeID("layer"), d1);

        executeAction(stringIDToTypeID("make"), d, DialogModes.NO);

        }

    catch (e) { alert(e); throw(e); }

    }

function place_file(file)

    {

    try {

        var d = new ActionDescriptor();

        d.putPath(stringIDToTypeID("file"), file);

        var doc = executeAction(stringIDToTypeID("openViewlessDocument"), d, DialogModes.NO).getData(stringIDToTypeID("document"));

        var d = new ActionDescriptor();

        var l = new ActionList();

        l.putPath(file);

        d.putList(stringIDToTypeID("fileList"), l);

        var l = new ActionList();

        l.putData(doc);

        d.putList(stringIDToTypeID("viewlessDoc"), l);

        executeAction(stringIDToTypeID("addLayerFromViewlessDoc"), d, DialogModes.NO);

        }

    catch (e) { alert(e); throw(e); }

    }

imaginereAuthor
New Participant
February 11, 2019

WOW! WOW! WOW! that is super exactly what I needed, you're a genius thank you!!! r-bin

joelc22025337
New Participant
December 1, 2019

Hey Gents!

I'm really interested in this script but I don't know anything about coding at all. I'm interested in getting this script to work because it would allow me to export render layers from Maya and import them as photoshop layers with the folder name as the group name. It sounds perfect for my workflow. Any chance you can point me in the right direction to getting this script?

Thanks so much!
*edit remove name

JJMack
Community Expert
Community Expert
December 5, 2018

Photoshop scripting shoul be able to do that.  Once you write the script it will be easy.

Write you own file stacking Script. Once you have stack the file select the layer you want to group and use action manager code to create a new layer group from the select.  You know menu Layer>New>Group From Layers... . You could stack a sub-folder of image at a time and use the sub-folder name as the Layer group name. Then do the next sub-folder of image files. Till all folders of source files have been processed.

The Image Processors scripts have code in it to Process a file system tree structure of image files.  You could look at the code in these scripts to see how to follow a file system structure and process the image files in that structure. Instead of Saving Files you would stacking image and create layer groups. Also read Load File  into stack script to see how it stacks image files into layers.

You will not find an other image editor that can be scripted as well as Photoshop.

JJMack
imaginereAuthor
New Participant
February 8, 2019

Thank you JJMack for that reply, I did not know you could get access to the code of the script, that is a very valuable tip and way of approaching the coding side of Adobe Photoshop.