Skip to main content
Participant
December 4, 2018
Answered

Layer group from folders or a text file

  • December 4, 2018
  • 2 replies
  • 4398 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); }

    }

Kukurykus
Legend
June 9, 2021

Good script, but I made first attempt on psd with smart object in CS6 and it crashed Photoshop. Then in same release tried with non smart objects layers and it worked. I didn't check whether 'placed layers' make it doesn't work yet. Maybe that's a problem of something else or bug in CS6. I'll see and let know.

Kukurykus
Legend
June 9, 2021

can you please tell me how to use this script? 

 

Do i need to copy it to a file and save it as a script and then import it as a script in PS?

 

It would be awesome if you could help a felloow photoshop guy in Santa Barbara, CA 🙂 

 

Thanks 

Albert


Yes, save to .jsx and run with Photoshop.

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
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.