Skip to main content
Known Participant
August 12, 2018
Answered

Group Selected Layers

  • August 12, 2018
  • 4 replies
  • 4738 views

Is it possible to group all selected layers individually? Each layer would remain within the respective group. All help will be treasured.

This topic has been closed for replies.
Correct answer Stephen Marsh

but, but 🙂 can you make it to work for each layer when selected  multiple layers? 


Try this:

 

#target photoshop

function main() {

    ///// Process selected layers - from jazz-y /////
    var s2t = stringIDToTypeID;
    (r = new ActionReference()).putProperty(s2t('property'), p = s2t('targetLayersIDs'));
    r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
    var lrs = executeActionGet(r).getList(p),
        sel = new ActionReference();
    for (var i = 0; i < lrs.count; i++) {
        sel.putIdentifier(s2t('layer'), p = lrs.getReference(i).getIdentifier(s2t('layerID')));
        (r = new ActionReference()).putIdentifier(s2t('layer'), p);
        (d = new ActionDescriptor()).putReference(s2t("target"), r);
        executeAction(s2t('select'), d, DialogModes.NO);
    //////////

        var layerName = activeDocument.activeLayer.name;
        setFromLayer("[Merge]" + layerName);
        setFromLayer("[Bone]" + layerName);

        ///// Functions /////

        function setFromLayer(layNme) {
            function s2t(s) {
                return app.stringIDToTypeID(s);
            }
            var descriptor = new ActionDescriptor();
            var descriptor2 = new ActionDescriptor();
            var reference = new ActionReference();
            var reference2 = new ActionReference();
            reference.putClass(s2t("layerSection"));
            descriptor.putReference(s2t("null"), reference);
            reference2.putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"));
            descriptor.putReference(s2t("from"), reference2);
            descriptor2.putString(s2t("name"), layNme);
            descriptor.putObject(s2t("using"), s2t("layerSection"), descriptor2);
            executeAction(s2t("make"), descriptor, DialogModes.NO);
        }
    }
}
app.activeDocument.suspendHistory("Undo Script", "main()");

4 replies

Greg von Neulich
Known Participant
May 3, 2023

we made a Photoshop plug-in that can do this. The unlimited version is 12$. 

 

Full Version (12$):
https://exchange.adobe.com/apps/cc/c36e02fb/swift


Trial Version (Limited to 5 Actions):
https://exchange.adobe.com/apps/cc/412c1dcd/swift-trial-version

 

Its also a big help with getting the layers selected.

Participating Frequently
November 3, 2022

Helo!

 

I need script that will make group from a layer and another group from that group with prefixes.  

for example result will look like this:

 

is that possible?

Stephen Marsh
Community Expert
Community Expert
November 3, 2022

@aleksanderk54154416 – Something like this?

 

var layerName = activeDocument.activeLayer.name;
setFromLayer("[Merge]" + layerName);
setFromLayer("[Bone]" + layerName);

function setFromLayer(layNme) {
	function s2t(s) {
		return app.stringIDToTypeID(s);
	}
	var descriptor = new ActionDescriptor();
	var descriptor2 = new ActionDescriptor();
	var reference = new ActionReference();
	var reference2 = new ActionReference();
	reference.putClass( s2t( "layerSection" ));
	descriptor.putReference( s2t( "null" ), reference );
	reference2.putEnumerated( s2t( "layer" ), s2t( "ordinal" ), s2t( "targetEnum" ));
	descriptor.putReference( s2t( "from" ), reference2 );
	descriptor2.putString( s2t( "name" ), layNme );
	descriptor.putObject( s2t( "using" ), s2t( "layerSection" ), descriptor2 );
	executeAction( s2t( "make" ), descriptor, DialogModes.NO );
}

 

A prompt could be added so that you type in the name of the prefix into a GUI, rather than having it hard-coded.

Participating Frequently
November 3, 2022

It works man! I am going to cry now :))

 

thanx a lot

Participant
September 27, 2019

Hi all,

 

Is it possible to make a script or action that both group and name the group the file name at the same time?

Example:

 

Layer01

Layer02

Layer02

 

I want to group them and name each group the layer names.

(exactly the same name as the layer or image)

 

Group name:

Layer01

Layer02

Layer03

Stephen Marsh
Community Expert
Community Expert
September 27, 2019

Try this:

 

 

#target Photoshop

// community.adobe.com/t5/Photoshop/Action-Script-to-group-layers-and-naming/td-p/10635514

// Select all layers - Except Background "Image" Layer
app.runMenuItem(stringIDToTypeID('selectAllLayers')); 

/*
// Select all layers...
var actionDescriptor = new ActionDescriptor();
var actionReference = new ActionReference();
actionReference.putEnumerated( charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );
actionDescriptor.putReference( charIDToTypeID( "null" ), actionReference );
executeAction( stringIDToTypeID( "selectAllLayers" ), actionDescriptor, DialogModes.NO );
*/

/*
// Select all layers  
// =======================================================  
var idselectAllLayers = stringIDToTypeID( "selectAllLayers" );  
var desc14 = new ActionDescriptor();  
var idnull = charIDToTypeID( "null" );  
var ref8 = new ActionReference();  
var idLyr = charIDToTypeID( "Lyr " );  
var idOrdn = charIDToTypeID( "Ordn" );  
var idTrgt = charIDToTypeID( "Trgt" );  
ref8.putEnumerated( idLyr, idOrdn, idTrgt );  
desc14.putReference( idnull, ref8 );  
executeAction( idselectAllLayers, desc14, DialogModes.NO );  
*/

// community.adobe.com/t5/Photoshop/Group-Selected-Layers/td-p/9983092#10555610
function sTT(v) {return stringIDToTypeID(v)}
(ref = new ActionReference()).putEnumerated
(sTT('document'), sTT('ordinal'), sTT('targetEnum'));
cnt = (lst = executeActionGet(ref).getList(sTT('targetLayersIDs'))).count
for(i = 0; i < cnt;) {
     id = lst.getReference(i++).getIdentifier();
     (ref1 = new ActionReference()).putClass(sTT('layerSection'));
     (dsc1 = new ActionDescriptor()).putReference(sTT('null'), ref1);
     (ref2 = new ActionReference()).putIdentifier(sTT('layer'), id)
     dsc1.putReference(sTT('from'), ref2);
     nme = executeActionGet(ref2).getString(sTT('name'));
     (dsc2 = new ActionDescriptor()).putString(sTT('name'), nme)
     dsc1.putObject(sTT('using'), sTT('layerSection'), dsc2)
     executeAction(sTT('make'), dsc1)
}

 

Participant
September 15, 2021

Hi, thanks for the script! But could you help me? I want to add a mask to the name. To rename the group like this: [slovo:name_layer]

Kukurykus
Legend
August 12, 2018

CC 2018:

function sTT(v) {return stringIDToTypeID(v)}

(ref = new ActionReference()).putEnumerated

(sTT('document'), sTT('ordinal'), sTT('targetEnum'));

cnt = (lst = executeActionGet(ref).getList(sTT('targetLayersIDs'))).count

for(i = 0; i < cnt;) {

     id = lst.getReference(i++).getIdentifier();

     (ref1 = new ActionReference()).putClass(sTT('layerSection'));

     (dsc1 = new ActionDescriptor()).putReference(sTT('null'), ref1);

     (ref2 = new ActionReference()).putIdentifier(sTT('layer'), id)

     dsc1.putReference(sTT('from'), ref2);

     nme = executeActionGet(ref2).getString(sTT('name'));

     (dsc2 = new ActionDescriptor()).putString(sTT('name'), nme)

     dsc1.putObject(sTT('using'), sTT('layerSection'), dsc2)

     executeAction(sTT('make'), dsc1)

}

Ps-DesignAuthor
Known Participant
August 12, 2018

Phenomenal your script Kukurykus

You are a genius. Thank you.