5 Replies Latest reply on Aug 25, 2016 11:51 PM by arteangelus

    How to delete every layer except of selected layer?

    arteangelus Level 1

      Hi,

      I would like to delete all layers including background and layer sets except of selected layer.

      It seemed easy first but it stops... I tried several ways; What am I doing wrong ?

       

      #target photoshop
      cTID = function(s) { return app.charIDToTypeID(s); };
      sTID = function(s) { return app.stringIDToTypeID(s); };
      
      app.bringToFront();
      var doc = app.activeDocument;
      var currentLayer = doc.activeLayer; // remember the selected layer
      var selectedlayername = currentLayer.name;
      currentLayer.move( activeDocument, ElementPlacement.PLACEATBEGINNING );  
      
        function DeleteLayer(layername) {  var desc1 = new ActionDescriptor();
          var ref1 = new ActionReference();   ref1.putName(cTID('Lyr '), layername);
          desc1.putReference(cTID('null'), ref1);     var list1 = new ActionList();
          list1.putInteger(17);  desc1.putList(cTID('LyrI'), list1);  executeAction(cTID('Dlt '), desc1, DialogModes.NO);
        };
      
      function DeleteBackground() { var desc1 = new ActionDescriptor();  var ref1 = new ActionReference();   ref1.putProperty(cTID('Lyr '), cTID('Bckg')); desc1.putReference(cTID('null'), ref1);     var list1 = new ActionList();     list1.putInteger(1);   desc1.putList(cTID('LyrI'), list1);    executeAction(cTID('Dlt '), desc1, DialogModes.NO); };
      
      function initial () {
      for(var i = 0 ; i < doc.layers.length;i++){    if (doc.layers[i].isBackgroundLayer == true ) {  DeleteBackground();} }
      var layerNum = doc.layers.length;
      for(var  s = 1 ; s <= layerNum;s++){
         if ( doc.layers[s].name != selectedlayername ) {   
             DeleteLayer (doc.layers[s].name);
             }
         else if ( doc.layers[s].name == selectedlayername ) {
             }
         }
      }
      
      initial();
      

       

      Thank you,

      Best Regards.

        • 1. Re: How to delete every layer except of selected layer?
          JJMack Most Valuable Participant

          I think it may be easier to open a new document with a transparent canvas the size of the current documents canvas then duplicate the selected layers into that new document.  Layers names may not be unique and if you start deleting layers layers positions in the stack change. The selected layer may no longer be selected.  If you noted the selected layer name there may be more than one layer with the same name which do you delete which do you keep. It not an easy thing to program.

           

          If you make that new document you could delete all layers but one copy the new document layers back in close no save the new document and the delete the bottom two layer..

          • 2. Re: How to delete every layer except of selected layer?
            Chuck Uebele Adobe Community Professional & MVP

            The reason yours may not be working is that you're using the actual length of the layers in your loop, and that changes as you delete the layers. You need to assign that number to a variable that will not change as you delete the layers. Try this script:

             

            #target photoshop
            var doc = activeDocument;
            var curLayer = doc.activeLayer
            curLayer.isBackgroundLayer = false;
            curLayer.move (doc.layers[0],ElementPlacement.PLACEBEFORE);
            var numLayers = doc.layers.length
            for(var i=1;i<numLayers;i++){
                doc.layers[1].remove();
                }
            
            3 people found this helpful
            • 3. Re: How to delete every layer except of selected layer?
              Jarda Bereza Level 4

              If you want faster performance, then I would recomend you

              1) Remeber your selected layer

               

              2) Try unlock background

              try{

              var idsetd = charIDToTypeID( "setd" );

                  var desc24 = new ActionDescriptor();

                  var idnull = charIDToTypeID( "null" );

                      var ref13 = new ActionReference();

                      var idLyr = charIDToTypeID( "Lyr " );

                      var idBckg = charIDToTypeID( "Bckg" );

                      ref13.putProperty( idLyr, idBckg );

                  desc24.putReference( idnull, ref13 );

                  var idT = charIDToTypeID( "T   " );

                      var desc25 = new ActionDescriptor();

                      var idOpct = charIDToTypeID( "Opct" );

                      var idPrc = charIDToTypeID( "#Prc" );

                      desc25.putUnitDouble( idOpct, idPrc, 100.000000 );

                      var idMd = charIDToTypeID( "Md  " );

                      var idBlnM = charIDToTypeID( "BlnM" );

                      var idNrml = charIDToTypeID( "Nrml" );

                      desc25.putEnumerated( idMd, idBlnM, idNrml );

                  var idLyr = charIDToTypeID( "Lyr " );

                  desc24.putObject( idT, idLyr, desc25 );

                  var idLyrI = charIDToTypeID( "LyrI" );

                  desc24.putInteger( idLyrI, 3229 );

              executeAction( idsetd, desc24, DialogModes.NO );}

              catch(){;}

               

              3) Choose menu item "Select > All layers"

              var idselectAllLayers = stringIDToTypeID( "selectAllLayers" );

                  var desc20 = new ActionDescriptor();

                  var idnull = charIDToTypeID( "null" );

                      var ref9 = new ActionReference();

                      var idLyr = charIDToTypeID( "Lyr " );

                      var idOrdn = charIDToTypeID( "Ordn" );

                      var idTrgt = charIDToTypeID( "Trgt" );

                      ref9.putEnumerated( idLyr, idOrdn, idTrgt );

                  desc20.putReference( idnull, ref9 );

              executeAction( idselectAllLayers, desc20, DialogModes.NO );

               

              4) Unlock all locks on this selection Photoshop: Javascript - can't unlock folder with script which was generated by ScriptListener | Photoshop Family Custome…

               

              5) Deselect layer you remebered in variable

               

              6) Delete

               

              Loops are incredible slow if call some in Photoshop method in each step. This algorithm is without loop.

              1 person found this helpful
              • 4. Re: How to delete every layer except of selected layer?
                natrev Level 3

                Hi arteangelus,

                 

                Try this Code...

                 

                #target photoshop  
                var docRef = app.activeDocument;  
                var layName = docRef.activeLayer.name  
                var l = Number(docRef.layers.length)+1;
                while(--l){
                    if(docRef.layers[l-1].name!=layName && docRef.layers[l-1].name!="Background"){
                        docRef.layers[l-1].remove();
                    }
                else{continue;}
                }
                

                 

                -yajiv

                1 person found this helpful
                • 5. Re: How to delete every layer except of selected layer?
                  arteangelus Level 1

                  Thank you natrev,

                   

                  I have combined your script with Delete background function as well so here is a working example if someone needs it;

                   

                  #target photoshop 
                  cTID = function(s) { return app.charIDToTypeID(s); };
                  sTID = function(s) { return app.stringIDToTypeID(s); };
                  
                  var docRef = app.activeDocument; 
                  var layName = docRef.activeLayer.name 
                  function DeleteBackground() { var desc1 = new ActionDescriptor();  var ref1 = new ActionReference();  ref1.putProperty(cTID('Lyr '), cTID('Bckg')); desc1.putReference(cTID('null'), ref1);    var list1 = new ActionList();    list1.putInteger(1);  desc1.putList(cTID('LyrI'), list1);    executeAction(cTID('Dlt '), desc1, DialogModes.NO); };
                  
                  for(var i = 0 ; i < docRef.layers.length;i++){
                      if (docRef.layers[i].isBackgroundLayer == true ) {  DeleteBackground();} }
                  
                  var l = Number(docRef.layers.length)+1;
                  while(--l){
                      if(docRef.layers[l-1].name!=layName && docRef.layers[l-1].name!="Background"){
                          docRef.layers[l-1].remove();
                      }
                  else{continue;}
                  }