Skip to main content
Mohamed Hameed21513110
Inspiring
September 20, 2022
Answered

splits layers and puts them into groups

  • September 20, 2022
  • 2 replies
  • 821 views

Greetings to all for the most wonderful forum, which serves many of its followers


I am working on a file with more than 500 layers

 

I want a script that splits layers and puts them into groups based on an input value

For example, I want to set a value for example 50 to choose 50 layers and place them inside a group named with the number of layers
--- Then select all the remaining layers and insert a value for example 30 to choose 30 layers and place them within a group named with the number of layers..... etc.

This topic has been closed for replies.
Correct answer Charu Rajput

@Mohamed Hameed21513110 

You can try the following script

 

deSelectAllLayers();
var _layers = app.activeDocument.artLayers;
var _name = prompt('Enter number : ', '');
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);
}

 

 

Reference links

https://community.adobe.com/t5/photoshop-ecosystem-discussions/photoshop-script-how-to-select-multi-layers-by-size-cm-or-inch/m-p/12550089/page/3#M602644

 

https://community.adobe.com/t5/photoshop-ecosystem-discussions/how-can-i-deselect-all-layers/td-p/9658958#:~:text=%3E%20Application%20Menus'.-,In%20the%20part%20below%2C%20go%20to%20'Select%20%3E%20Deselect%20Layers,Ctrl%2DAlt%2Dd'.&text=Now%20this%2C%20is%20the%20correct%20answer.

 

Note: The above script will not take care of nested structure of the layers.

 

2 replies

Charu Rajput
Community Expert
Community Expert
September 20, 2022

Hi @Mohamed Hameed21513110 

When you say, you set a value 50 to choose 50 layers, what does it means

1. 50 layers alreday selected?

2. you want to select 50 layers. if yes, which 50 layers, first 50 , last 50 or alternative. How you will decide which 50 layers.

 

If you can show some samples that will be helpful for us to guide you in right direction.

 

Best regards
Mohamed Hameed21513110
Inspiring
September 20, 2022

@Charu Rajput 

To clearly explain what is required
I have a file with for example 500 layers or more and they are all the same size
I want to make groups for the layers, but with different numbers for each group

For example, I want to choose 50 layers and put them in a group named 50 layers
Choose 30 layers and place them in a group named 30 layers

-- As for the selection of layers, the layers are selected from top to bottom..with the exclusion of the layers and groups that have been created

 

Best regards
Charu Rajput
Community Expert
Charu RajputCommunity ExpertCorrect answer
Community Expert
September 20, 2022

@Mohamed Hameed21513110 

You can try the following script

 

deSelectAllLayers();
var _layers = app.activeDocument.artLayers;
var _name = prompt('Enter number : ', '');
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);
}

 

 

Reference links

https://community.adobe.com/t5/photoshop-ecosystem-discussions/photoshop-script-how-to-select-multi-layers-by-size-cm-or-inch/m-p/12550089/page/3#M602644

 

https://community.adobe.com/t5/photoshop-ecosystem-discussions/how-can-i-deselect-all-layers/td-p/9658958#:~:text=%3E%20Application%20Menus'.-,In%20the%20part%20below%2C%20go%20to%20'Select%20%3E%20Deselect%20Layers,Ctrl%2DAlt%2Dd'.&text=Now%20this%2C%20is%20the%20correct%20answer.

 

Note: The above script will not take care of nested structure of the layers.

 

Best regards
ActionNodes
Participant
September 20, 2022

Hello, you can try ActionNodes to create such an action yourself. It's pretty easy.