Skip to main content
Known Participant
July 12, 2021
Answered

Remove All Group Without any contents

  • July 12, 2021
  • 3 replies
  • 2619 views

Is that possible to remove all Group only without any contents ?

This topic has been closed for replies.
Correct answer r-bin
You need to more clearly express your thought (not in "two words")
ungroup_folder(activeDocument);

function ungroup_folder(f)
    {
    try {
        for (var i = f.layerSets.length-1; i >= 0; i--)
            {
            ungroup_folder(f.layerSets[i]);
            }

        if (f.typename == "LayerSet") 
            {
            cTID = function(s) { return app.charIDToTypeID(s); };
            sTID = function(s) { return app.stringIDToTypeID(s); }; 

            var desc1 = new ActionDescriptor();
            var ref1 = new ActionReference();
            //ref1.putEnumerated(cTID('Lyr '), cTID('Ordn'), cTID('Trgt'));
            ref1.putIdentifier(cTID('Lyr '), f.id);
            desc1.putReference(cTID('null'), ref1);
            desc1.putBoolean(sTID("deleteContained"), false);
            executeAction(cTID('Dlt '), desc1, DialogModes.NO);
            }
        }
    catch (e) { alert(e); }        
    }

3 replies

Chuck Uebele
Community Expert
Community Expert
July 13, 2021

As far as the translation issue, I would post your questions and replies in both your native language and in English. Maybe someone can then better translate. I would also use more screen shots to show want you want.

Chuck Uebele
Community Expert
Community Expert
July 13, 2021

You can also try this. It seems to delete all empty groups and will delete nested empty groups.

 

 

#target photoshop
var doc = activeDocument;
var useList = [];
var layerSets = 0
try{doc.backgroundLayer}
catch(e){layerSets=1}
var layerList = getLayerSetsData();


for (var i=0;i<layerList.length;i++){
    if(layerList[i].type == 7){
        useList.push(layerList[i].id)};
            }         
for(var i=0;i<useList.length;i++){
    multiSelectByIDs (useList[i]);
    if(doc.activeLayer.layers.length == 0){doc.activeLayer.remove()}
    }
    
function getLayerSetsData(){
    var lyrSets = [];
    
    while (true)
    {
        ref = new ActionReference();
        ref.putIndex(charIDToTypeID('Lyr '), layerSets);
        try
            {var d1 = executeActionGet(ref)}
        catch (err){
            break;
            };

        var c2t = function (s){return app.charIDToTypeID(s);};
        var s2t = function (s){return app.stringIDToTypeID(s);};
        var lyrSet = {};

        lyrSet.type = d1.getInteger(s2t("layerKind"));
        lyrSet.name = d1.getString(c2t("Nm  "));
        lyrSet.id = d1.getInteger(s2t("layerID"));
        
        lyrSets.push(lyrSet);
        layerSets++;
    }; 
    return lyrSets;
}; 

function doesIdExists( id ){// function to check if the id exists

   var res = true;
   var ref = new ActionReference();
   ref.putIdentifier(charIDToTypeID('Lyr '), id);
    try{var desc = executeActionGet(ref)}catch(err){res = false};
    return res;
}

function multiSelectByIDs(ids) {
    $.writeln(ids)
  if( ids.constructor != Array ) ids = [ ids ];
    var layers = new Array();
    var id54 = charIDToTypeID( "slct" );
    var desc12 = new ActionDescriptor();
    var id55 = charIDToTypeID( "null" );
    var ref9 = new ActionReference();
    for (var i = 0; i < ids.length; i++) {
       if(doesIdExists(ids[i]) == true){// a check to see if the id stil exists
           layers[i] = charIDToTypeID( "Lyr " );
           ref9.putIdentifier(layers[i], ids[i]);
       }
    }
    desc12.putReference( id55, ref9 );
    var id58 = charIDToTypeID( "MkVs" );
    desc12.putBoolean( id58, false );
    executeAction( id54, desc12, DialogModes.NO );
}

 

 

MXKSAuthor
Known Participant
July 13, 2021

No , I will remove Group Only Without any content :-

cTID = function(s) { return app.charIDToTypeID(s); };
sTID = function(s) { return app.stringIDToTypeID(s); }; 

 var desc1 = new ActionDescriptor();
    var ref1 = new ActionReference();
    ref1.putEnumerated(cTID('Lyr '), cTID('Ordn'), cTID('Trgt'));
    desc1.putReference(cTID('null'), ref1);
    desc1.putBoolean(sTID("deleteContained"), false);
    executeAction(cTID('Dlt '), desc1, DialogModes.NO);
Legend
July 12, 2021

Close to what you need:

File-> Scripts-> Delete All Empy Layers

 

For groups only, you need to edit the script

 

UPD. the code is taken from that script 

 

deleteAllEmptyLayerSets(activeDocument)

function deleteAllEmptyLayerSets(o){
    try{
        var bEmpty = true;
        for(var i = o.layerSets.length-1; 0 <= i; i--) {
            var layerSet = o.layerSets[i];
            if((layerSet.allLocked)||(0!=layerSet.linkedLayers.length)){
                continue; // skip locked or linked
            }
            if(deleteAllEmptyLayerSets(layerSet)){ // empty
                layerSet.remove(); // delete
            } else  bEmpty = false;      // not empty
        }
        if(0 != o.artLayers.length) bEmpty = false; // not empty
    }catch(e){
        ; // do nothing
    }
    return bEmpty;
}

 

MXKSAuthor
Known Participant
July 13, 2021

Yes @r-bin , Can You Modify this !

cTID = function(s) { return app.charIDToTypeID(s); };
sTID = function(s) { return app.stringIDToTypeID(s); }; 

 var desc1 = new ActionDescriptor();
    var ref1 = new ActionReference();
    ref1.putEnumerated(cTID('Lyr '), cTID('Ordn'), cTID('Trgt'));
    desc1.putReference(cTID('null'), ref1);
    desc1.putBoolean(sTID("deleteContained"), false);
    executeAction(cTID('Dlt '), desc1, DialogModes.NO);
MXKSAuthor
Known Participant
July 13, 2021

The first @r-bin  scirpt is then correct answer Of my question, why @r-bin   have said need to edit the script


Don't need to google translation because eveything is understanding , If you don't understand tell me which sentence is not clear ?