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

Photoshop action to group every 4 layers?

Community Beginner ,
Jan 30, 2023 Jan 30, 2023

Copy link to clipboard

Copied

Hello. Is it possible to script a photoshop action to group every 4 layers?

Seems simple enough but I am unable to do using the record functionality.

 

Thanks!

TOPICS
Actions and scripting

Views

307

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
Community Expert ,
Jan 30, 2023 Jan 30, 2023

Copy link to clipboard

Copied

It is possible, however, it is not elegant or bullet proof. You need to use relative keyboard shortcuts to select layers rather than selecting layers with the mouse.

 

Relative Layer Keycuts:

Select/target front (top) layer = Option + .
Select/target next layer up = Option + ]
Select/target next layer down = Option + [
Select/target back (bottom) layer = Option + ,

(For Windows, swap the Option for Alt)

 

You will also need to hold down the shift key when selecting the 4 layers to group.

 

A script would be better as it can use conditional logic. For example, what if the length/count of the layers is not evenly divisible by four?

 

A lot will depend on the structure of your layers and their visibility, also consistency between files will have a major impact.

 

Can you provide a screenshot of your layers panel?

 

Edit: This action groups 4 sets of 4 layers, so it must be run on a doc with 16 layers, otherwise the results are not as intended.

 

atn.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
Community Expert ,
Jan 31, 2023 Jan 31, 2023

Copy link to clipboard

Copied

It is doable to check using conditional action for Background layer or layer group but... my action which can be downloaded from here https://drive.google.com/file/d/1kaU22DnX-KeIy_Vi0SksbBT6s-ccvWGz/view?usp=share_link has some limitations which are described at the beginning of action: 

 

"This action is created to work with at the most 52 layers (or layers and layer groups summed up including and Background layer which is required) in the Layers panel. Another limitation of this action is that it will always select bottom most layer at the beginning and after checking for layer groups in the Layers panel. If you want to modify action you are welcome without restriction just beware of limitations and structure as it is."

 

Note: this action is recorded for training and showcase of capabilities in the first place. Please report any error or problem.

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 ,
Jan 30, 2023 Jan 30, 2023

Copy link to clipboard

Copied

Here is a script, it prompts for the number of layers to group. This could be hard-coded into the script if it was always 4.

 

This script needs to be run multiple times, so if there were 8 layers, the script would be run twice. 

 

/*
https://community.adobe.com/t5/photoshop-ecosystem-discussions/splits-layers-and-puts-them-into-groups/td-p/13211952
by Charu Rajput
*/

deSelectAllLayers();
var layLength = activeDocument.layers.length;
var _layers = app.activeDocument.artLayers;
var _name = prompt('Enter number: ', layLength);

if (_name) {
    _groupLength = Number(_name);
    if (!isNaN(_groupLength)) {
        if (_groupLength !== 0 && _groupLength <= _layers.length) {
            for (var i = 0; i < _groupLength; i++) {
                selectLayerByID(_layers[i].id, true);
            }
            groupSelectedLayers(_name);
        } else {
            alert('Either you entered 0 or entered number is higher than the number of layers present in the document');
        }
    } else {
        alert('Enter value is not the number');
    }
}

function groupSelectedLayers(theName) {
    var desc159 = new ActionDescriptor();
    var ref114 = new ActionReference();
    var idlayer = stringIDToTypeID("layer");
    var idordinal = stringIDToTypeID("ordinal");
    var idtargetEnum = stringIDToTypeID("targetEnum");
    var idnull = stringIDToTypeID("null");
    var idname = stringIDToTypeID("name");
    ref114.putEnumerated(idlayer, idordinal, idtargetEnum);
    desc159.putReference(idnull, ref114);
    desc159.putString(idname, "aaa");
    executeAction(stringIDToTypeID("groupLayersEvent"), desc159, DialogModes.NO);
    var desc63 = new ActionDescriptor();
    var ref37 = new ActionReference();
    ref37.putEnumerated(idlayer, idordinal, idtargetEnum);
    desc63.putReference(idnull, ref37);
    var desc64 = new ActionDescriptor();
    desc64.putString(idname, theName);
    desc63.putObject(stringIDToTypeID("to"), idlayer, desc64);
    executeAction(stringIDToTypeID("set"), desc63, DialogModes.NO);
}

function selectLayerByID(index, add) {
    add = undefined ? add = false : add;
    var ref = new ActionReference();
    ref.putIdentifier(charIDToTypeID("Lyr "), index);
    var desc = new ActionDescriptor();
    desc.putReference(charIDToTypeID("null"), ref);
    if (add) desc.putEnumerated(stringIDToTypeID("selectionModifier"), stringIDToTypeID("selectionModifierType"), stringIDToTypeID("addToSelection"));
    desc.putBoolean(charIDToTypeID("MkVs"), false);
    try {
        executeAction(charIDToTypeID("slct"), desc, DialogModes.NO);
    } catch (e) {
        alert(e.message);
    }
}

function deSelectAllLayers() {
    var idselectNoLayers = stringIDToTypeID("selectNoLayers");
    var desc26 = new ActionDescriptor();
    var idnull = charIDToTypeID("null");
    var ref3 = new ActionReference();
    var idLyr = charIDToTypeID("Lyr ");
    var idOrdn = charIDToTypeID("Ordn");
    var idTrgt = charIDToTypeID("Trgt");
    ref3.putEnumerated(idLyr, idOrdn, idTrgt);
    desc26.putReference(idnull, ref3);
    executeAction(idselectNoLayers, desc26, DialogModes.NO);
}

 

 

https://prepression.blogspot.com/2017/11/downloading-and-installing-adobe-scripts.html

 

 

EDIT: Here is a modified version to loop over all top-level artLayers so that it is fully automated. There is no error checking if there are more or less than 4 evenly divisible layers, so if there are "left over" layers they will remain ungrouped.

 

for (var i = 0; i < activeDocument.artLayers.length; i++) {
    layersToGroups();
}

function layersToGroups() {
    /*
https://community.adobe.com/t5/photoshop-ecosystem-discussions/splits-layers-and-puts-them-into-groups/td-p/13211952
by Charu Rajput
*/

    deSelectAllLayers();
    //var layLength = activeDocument.layers.length;
    var _layers = app.activeDocument.artLayers;
    //var _name = prompt('Enter number: ', layLength);
    var _name = 4;

    if (_name) {
        _groupLength = Number(_name);
        if (!isNaN(_groupLength)) {
            if (_groupLength !== 0 && _groupLength <= _layers.length) {
                for (var i = 0; i < _groupLength; i++) {
                    selectLayerByID(_layers[i].id, true);
                }
                groupSelectedLayers(_name);
            } else {
                alert('Either you entered 0 or entered number is higher than the number of layers present in the document');
            }
        } else {
            alert('Enter value is not the number');
        }
    }

    function groupSelectedLayers(theName) {
        var desc159 = new ActionDescriptor();
        var ref114 = new ActionReference();
        var idlayer = stringIDToTypeID("layer");
        var idordinal = stringIDToTypeID("ordinal");
        var idtargetEnum = stringIDToTypeID("targetEnum");
        var idnull = stringIDToTypeID("null");
        var idname = stringIDToTypeID("name");
        ref114.putEnumerated(idlayer, idordinal, idtargetEnum);
        desc159.putReference(idnull, ref114);
        desc159.putString(idname, "aaa");
        executeAction(stringIDToTypeID("groupLayersEvent"), desc159, DialogModes.NO);
        var desc63 = new ActionDescriptor();
        var ref37 = new ActionReference();
        ref37.putEnumerated(idlayer, idordinal, idtargetEnum);
        desc63.putReference(idnull, ref37);
        var desc64 = new ActionDescriptor();
        desc64.putString(idname, theName);
        desc63.putObject(stringIDToTypeID("to"), idlayer, desc64);
        executeAction(stringIDToTypeID("set"), desc63, DialogModes.NO);
    }

    function selectLayerByID(index, add) {
        add = undefined ? add = false : add;
        var ref = new ActionReference();
        ref.putIdentifier(charIDToTypeID("Lyr "), index);
        var desc = new ActionDescriptor();
        desc.putReference(charIDToTypeID("null"), ref);
        if (add) desc.putEnumerated(stringIDToTypeID("selectionModifier"), stringIDToTypeID("selectionModifierType"), stringIDToTypeID("addToSelection"));
        desc.putBoolean(charIDToTypeID("MkVs"), false);
        try {
            executeAction(charIDToTypeID("slct"), desc, DialogModes.NO);
        } catch (e) {
            alert(e.message);
        }
    }

    function deSelectAllLayers() {
        var idselectNoLayers = stringIDToTypeID("selectNoLayers");
        var desc26 = new ActionDescriptor();
        var idnull = charIDToTypeID("null");
        var ref3 = new ActionReference();
        var idLyr = charIDToTypeID("Lyr ");
        var idOrdn = charIDToTypeID("Ordn");
        var idTrgt = charIDToTypeID("Trgt");
        ref3.putEnumerated(idLyr, idOrdn, idTrgt);
        desc26.putReference(idnull, ref3);
        executeAction(idselectNoLayers, desc26, DialogModes.NO);
    }
}

 

 

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 Beginner ,
Jan 31, 2023 Jan 31, 2023

Copy link to clipboard

Copied

Wow I really appreciate the help. I'll give it a go. Attached is the layer sequenceScreenshot 2023-01-31 at 10.08.59 AM.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
Community Expert ,
Jan 31, 2023 Jan 31, 2023

Copy link to clipboard

Copied

So you always have only 4 layer to group into layer group? It is quite easy then. Here is example action https://drive.google.com/file/d/1U4nfVPy5st8xUUaFJwBPCQQtSR8GHtnG/view?usp=share_link

Requirement is that you have three layers above selected layer before running action. So select first layer in sequence (Layer 236 in screenshot above) then run action. Action will select next three layers with add to selection and put all 4 layers in layer group. You can run action using Shift + F2. Shortcut can be changed.

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 Beginner ,
Feb 01, 2023 Feb 01, 2023

Copy link to clipboard

Copied

LATEST

This works like a charm! The only caveat is that I had to remove the background layer as it threw off the count, but once I did that it was perfect. Thank you.

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 ,
Jan 31, 2023 Jan 31, 2023

Copy link to clipboard

Copied

Are all the Layers top-level or are existing Groups involved? 

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 Beginner ,
Jan 31, 2023 Jan 31, 2023

Copy link to clipboard

Copied

No other groups are involved

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