Skip to main content
Known Participant
Aug 12, 2018 2:51 AM UTC
Answered

Group Selected Layers

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

This topic is closed to new replies. Start a new post to keep the conversation going.
Stephen Marsh
Stephen MarshCorrect answer
Community Expert
Nov 3, 2022 10:34 PM UTC

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()");

21 replies

Greg von Neulich
Known Participant
May 3, 2023 8:22 PM UTC

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.

Translate
Participating Frequently
Nov 3, 2022 11:42 AM UTC

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?

Translate
Stephen Marsh
Community Expert
Community Expert
Nov 3, 2022 12:39 PM UTC

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

Translate
Participating Frequently
Nov 3, 2022 1:07 PM UTC

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

 

thanx a lot

Translate
Participant
Sep 27, 2019 8:12 AM UTC

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

Translate
Stephen Marsh
Community Expert
Community Expert
Sep 27, 2019 11:04 AM UTC

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)
}

 

Translate
Participant
Sep 15, 2021 7:56 PM UTC

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]

Translate
Kukurykus
Legend
Aug 12, 2018 3:42 AM UTC

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)

}

Translate
Ps-DesignAuthor
Known Participant
Aug 12, 2018 12:24 PM UTC

Phenomenal your script Kukurykus

You are a genius. Thank you.

Translate