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

group all layers except the back layer

Explorer ,
Jul 22, 2021 Jul 22, 2021

Copy link to clipboard

Copied

I know this has been asked quite alot and I have tried many code snippets from here but I just can't get it to work how I want, hope somebody can help.

 what I want to achieve it move all the Layers to a new group expect the back layer. I tried to use action for this: with action I select all layers then create a group from selected layers, this works find if I have a locked backround layer, but when I have a normal layer it will be also moved inside the group as well.

 

Here what I got so far but I can't figure out how to exculde the most back layer.

 

 

#target photoshop

var doc = app.activeDocument;

for (var i = 0; i < doc.layers.length; i++) {
    try {
        doc.activeLayer = doc.layers[i];
        if (doc.layers = i - 1) {
            SelectAllLayers();
            addLayeSet();
        }
    } catch (err) {}
    return;
}

function SelectAllLayers() {
    try {
        app.runMenuItem(stringIDToTypeID("selectAllLayers"));
    } catch (err) {}
    return;
}


function addLayeSet() {
    try {
        var newGroup = doc.layerSets.add();
        newGroup.name = "Adjustments";
        activeDocument.activeLayer.move(doc.layerSets["Adjustments"], ElementPlacement.INSIDE);
    } catch (err) {}
    return
}

 

 

TOPICS
Actions and scripting , macOS

Views

281

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

correct answers 2 Correct answers

Community Expert , Jul 22, 2021 Jul 22, 2021

Does this help? 

// group all but lowermost layer;
// 2021, use it at your own risk;
if (app.documents.length > 0) {
    theStuff ();
    };
////////////////////////////////////
function theStuff () {
// get number of layers;
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 
var docDesc = executeActionGet(ref);
var theNumber = docDesc.getInteger(stringIDToTypeID("numberOfLayers"));
var hasBackground = docDesc.getBoolean
...

Votes

Translate

Translate
LEGEND , Jul 22, 2021 Jul 22, 2021
lrs = [].slice.call(activeDocument.layers)
sTT = stringIDToTypeID; ref = new ActionReference()
while(lrs.length > 1) ref.putIdentifier(sTT('layer'), lrs.shift().id);
(dsc = new ActionDescriptor()).putReference(sTT('null'), ref)
dsc.putBoolean(sTT('makeVisible'), false);
executeAction(sTT('select'), dsc);

(ref1 = new ActionReference()).putClass(sTT('layerSection'));
(dsc1 = new ActionDescriptor()).putReference(sTT('null'), ref1), nme = 'Adjustements';
(ref2=new ActionReference()).putEnumerated(s
...

Votes

Translate

Translate
Adobe
Community Expert ,
Jul 22, 2021 Jul 22, 2021

Copy link to clipboard

Copied

Does this help? 

// group all but lowermost layer;
// 2021, use it at your own risk;
if (app.documents.length > 0) {
    theStuff ();
    };
////////////////////////////////////
function theStuff () {
// get number of layers;
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 
var docDesc = executeActionGet(ref);
var theNumber = docDesc.getInteger(stringIDToTypeID("numberOfLayers"));
var hasBackground = docDesc.getBoolean(stringIDToTypeID("hasBackgroundLayer"));
// select layers;
if (hasBackground == true) {var theX = 1} else {var theX = 2};
selectLayerByIndex(theNumber,false);
for (var m = theX; m <= theNumber-1; m++) {
    selectLayerByIndex(m,true)
};
// group layers;
var desc159 = new ActionDescriptor();
var ref114 = new ActionReference();
ref114.putEnumerated( charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );
desc159.putReference( charIDToTypeID( "null" ), ref114 );
executeAction( stringIDToTypeID( "groupLayersEvent" ), desc159, DialogModes.NO );
};
////////////////////////////////////
// by mike hale, via paul riggott;
function selectLayerByIndex(index,add){ 
    add = undefined ? add = false:add 
    var ref = new ActionReference();
        ref.putIndex(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); 
    }
    };

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
Explorer ,
Jul 22, 2021 Jul 22, 2021

Copy link to clipboard

Copied

LATEST

Thank you it did help, I appericate it a lot!

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
LEGEND ,
Jul 22, 2021 Jul 22, 2021

Copy link to clipboard

Copied

lrs = [].slice.call(activeDocument.layers)
sTT = stringIDToTypeID; ref = new ActionReference()
while(lrs.length > 1) ref.putIdentifier(sTT('layer'), lrs.shift().id);
(dsc = new ActionDescriptor()).putReference(sTT('null'), ref)
dsc.putBoolean(sTT('makeVisible'), false);
executeAction(sTT('select'), dsc);

(ref1 = new ActionReference()).putClass(sTT('layerSection'));
(dsc1 = new ActionDescriptor()).putReference(sTT('null'), ref1), nme = 'Adjustements';
(ref2=new ActionReference()).putEnumerated(sTT('layer'),sTT('ordinal'),sTT('targetEnum'))
dsc1.putReference(sTT('from'),ref2);(dsc2=new ActionDescriptor()).putString(sTT('name'),nme)
dsc1.putObject(sTT('using'), sTT('layerSection'), dsc2), executeAction(sTT('make'), dsc1)

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
Explorer ,
Jul 22, 2021 Jul 22, 2021

Copy link to clipboard

Copied

That is excatly what I wanted Thanks a lot! 

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