15 Replies Latest reply on May 21, 2016 3:23 PM by Jarda Bereza
      • 1. Re: Delete empty group folders?
        Paul Riggott Level 6

        Could you test this please...

         

         

        #target Photoshop
        app.bringToFront();
        main();
        function main(){
        if(!documents.length) return;
        //iterate 5 loops incase of nested layersets
        for(var t = 0; t<1;t++){
        var LayerSetLayers=[];
        var lSets = getLayerSets();
        for(var z in lSets){
        var lset = getChildIndex(Number(lSets[z]), true );
        LayerSetLayers[Number(lSets[z])] = lset;
        }
        var emptySets = new Array();
        for(var a in lSets){
            if(LayerSetLayers[Number(lSets[a])].length <1) emptySets.push(getLayerID(Number(lSets[a])));
            }
            for(var g in emptySets){
              if(selectLayerById(Number(emptySets[g]))) deleteLayer();
                }
            }
        };
        function getLayerID(IDX){
        var ref = new ActionReference();
        if(IDX == undefined){
        ref.putEnumerated( charIDToTypeID('Lyr '),charIDToTypeID('Ordn'),charIDToTypeID('Trgt') ); 
        }else{
            ref.putIndex( charIDToTypeID('Lyr '), IDX );
            }
        var desc = executeActionGet(ref);
        return desc.getInteger(stringIDToTypeID( 'layerID' ));
        };
        function selectLayerById(ID, add) {
            add = (add == undefined)  ? add = false : add;
         var ref = new ActionReference();
         ref.putIdentifier(charIDToTypeID('Lyr '), ID);
         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);
            return true;
            }catch(e){return false;}
        }
        function deleteLayer() {
        var desc = new ActionDescriptor();
        var ref = new ActionReference();
        ref.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
        desc.putReference( charIDToTypeID('null'), ref );
        try{
        executeAction( charIDToTypeID('Dlt '), desc, DialogModes.NO );
        }catch(e){}
        };
        function getLayerSets(){ 
           var ref = new ActionReference(); 
           ref.putEnumerated( charIDToTypeID('Dcmn'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') ); 
           var count = executeActionGet(ref).getInteger(charIDToTypeID('NmbL')) +1; 
           var Lsets=[];
        try{
            activeDocument.backgroundLayer;
        var i = 0; }catch(e){ var i = 1; };
           for(i;i<count;i++){ 
               if(i == 0) continue;
                ref = new ActionReference(); 
                ref.putIndex( charIDToTypeID( 'Lyr ' ), i );
                var desc = executeActionGet(ref);
                var layerName = desc.getString(charIDToTypeID( 'Nm  ' ));
                if(layerName.match(/^<\/Layer group/) ) continue;
                var layerType = typeIDToStringID(desc.getEnumerationValue( stringIDToTypeID( 'layerSection' )));
                var isLayerSet =( layerType == 'layerSectionContent') ? false:true;
                if(isLayerSet) Lsets.push(i);
           }; 
        return Lsets;
        };
        function getLayerLayerSectionByIndex( index ) {   
           var ref = new ActionReference(); 
           ref.putIndex(charIDToTypeID('Lyr '), index);
           return typeIDToStringID(executeActionGet(ref).getEnumerationValue(stringIDToTypeID('layerSection')));
        }; 
        function getLayerNameByIndex( index ) { 
            var ref = new ActionReference(); 
            ref.putIndex( charIDToTypeID( 'Lyr ' ), index );
            return executeActionGet(ref).getString(charIDToTypeID( 'Nm  ' ));
        };
        function skipNestedSets( layerIndex ){
           var isEnd = false;
           layerIndex = app.activeDocument.layers[app.activeDocument.layers.length-1].isBackgroundLayer ? layerIndex-2:layerIndex;
           while(!isEnd){
              layerIndex--;
              if( getLayerLayerSectionByIndex( layerIndex ) == 'layerSectionStart' ) layerIndex = skipNestedSets( layerIndex );
              isEnd = getLayerNameByIndex(layerIndex) == '</Layer group>' ? true:false;
           }
           return layerIndex-1;
        };
        function getChildIndex(idx, skipNested ){
           var layerSetIndex = idx;
           var isEndOfSet = false;
           var layerIndexArray = [];
           while(!isEndOfSet){
              layerSetIndex--;
              if( getLayerLayerSectionByIndex( layerSetIndex ) == 'layerSectionStart' && skipNested ){
                 layerSetIndex = skipNestedSets( layerSetIndex );
              }
          if(getLayerLayerSectionByIndex( layerSetIndex ) == undefined) break;
              isEndOfSet = getLayerNameByIndex(layerSetIndex) == '</Layer group>' ? true:false;
             if(!isEndOfSet ) layerIndexArray.push( layerSetIndex );
           }
           return layerIndexArray;
        };
        function selectLayerByIndex(index,add){ 
         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){}
        };
        
        
        
        3 people found this helpful
        • 2. Re: Delete empty group folders?
          Navarro Parker Level 3

          That's pretty amazing! Great job! It even gets rid of empty text layers. Very cool!

          • 3. Re: Delete empty group folders?
            marcolucidi

            uh, i looking for this action, i would delete empty folder in photoshop, but how i use this code in photoshop?

            • 4. Re: Delete empty group folders?
              JJMack Most Valuable Participant

              Its a Photoshop Script not an Action.  You save the code as a jsx file in any folder you want or in Photoshop's Presets Scripts.  If Saved in Photoshop's Presets Scripts the next time you start Photoshop you will see the script listed in menu File>Scripts>ScriptName...

              1 person found this helpful
              • 5. Re: Delete empty group folders?
                marcolucidi Level 1

                thanks jjmack I took your advice. But the script did not work at all, leaving some empty folders and when performed for the second time eliminates levels used

                • 6. Re: Delete empty group folders?
                  c.pfaffenbichler Level 9

                  If your files do not have too many Layers (including Groups) maybe you should pursue a DOM solution instead of AM code, that should be easier to do in case you do not have a lot of experience with Photoshop Scripting.

                  • 7. Re: Delete empty group folders?
                    marcolucidi Level 1

                    what is dom solution or Am code? i don't have experience with scripting, I was hoping to get by with the copy paste and save in jsx script made by others (as I did in this case)

                    • 8. Re: Delete empty group folders?
                      c.pfaffenbichler Level 9
                      what is dom solution or Am code?

                      Photoshop Document Object Model code is usually more easily read-/writeable than Action Manager code but often runs slower to perform the same task.

                       

                      I was hoping to get by with the copy paste and save in jsx script made by others

                      Unless someone takes an interest in the issue and takes the time to trouble-shoot the existing code for you or writes something new that may not be enough.

                      But as for trouble-shooting your description of the unintended behaviour may have to be more detailed.

                      • 9. Re: Delete empty group folders?
                        marcolucidi Level 1

                        hi c.pfaffenbichler, I use a lot photoshop to composite photography Web design, files and folders you have many levels. Configure the graphics it requires much manual labor, but I thought that script and actions can help me .... I'm looking for them this

                        • 10. Re: Delete empty group folders?
                          c.pfaffenbichler Level 9
                          but I thought that script and actions can help me

                          Actions are fairly limited, Scripts can achieve a lot more but if you need a custom solution for a particular problem you may have to delve into Scripting yourself – or wait to see if someone else will do the Script for you.

                          Do you have any JavaScript experience?

                          • 11. Re: Delete empty group folders?
                            marcolucidi Level 1

                            unfortunately I have no experience with any kind of code, I proceed by intuition, if it works use it, if it does not work I despair and I do everything manually;)

                            I was hoping to find a site where they shared many scripts for adobe suite, exist?

                            • 12. Re: Delete empty group folders?
                              c.pfaffenbichler Level 9

                              This is it pretty much, I guess, since ps-scripts.com is no longer properly online, but you could also check out

                              https://creative.adobe.com/addons

                               

                              As so often it applies that what you get out of X (Photoshop Scripting in this case) depends (to some extent at least) on what you put into it.

                               

                              Also maybe you should start a new thread.

                              • 13. Re: Delete empty group folders?
                                marcolucidi Level 1

                                thank you c.pfaffenbichler

                                • 14. Re: Delete empty group folders?
                                  c.pfaffenbichler Level 9

                                  This would be a DOM approach, but it may be fairly slow and, as a Group containing an empty Group is not empty itself, the result may not always be what you want.

                                  // 2015, use it at your own risk;
                                  #target photoshop
                                  var theLayers = collectEmptyGroups(app.activeDocument, []);
                                  alert (theLayers.join("\n"));
                                  for (var x = 0; x < theLayers.length; x++) {
                                    try {theLayers[x].remove()}
                                    catch (e) {}};
                                  ////// function collect all layers //////
                                  function collectEmptyGroups (theParent, allLayers) {
                                    if (!allLayers) {var allLayers = new Array}
                                    else {};
                                    var theNumber = theParent.layers.length - 1;
                                    for (var m = theNumber; m >= 0;m--) {
                                    var theLayer = theParent.layers[m];
                                  // apply the function to layersets;
                                    if (theLayer.typename == "ArtLayer") {
                                  // allLayers.push(theLayer)
                                    }
                                    else {
                                    allLayers = (collectEmptyGroups(theLayer, allLayers))
                                  // this line includes the empty layer groups;
                                  if (theLayer.layers.length == 0) {allLayers.push(theLayer)}
                                    }
                                    };
                                    return allLayers
                                    };
                                  
                                  • 15. Re: Delete empty group folders?
                                    Jarda Bereza Level 4

                                    I wrote my own version. Download here: Magic scripts for Photoshop

                                     

                                    It is relatively fast. Up to 17× faster than Adobe default script. But now it works only with PS CC 2015

                                    It also shows progressbar :-)