12 Replies Latest reply on Aug 23, 2014 9:21 AM by vishok75

    Remove not active layer

    vishok75

      i have a psd with a lot of layers. I'd like to remove all layer but not the first or the active one. Someone cuold help me to do this?

        • 1. Re: Remove not active layer
          c.pfaffenbichler Level 9

          This hardly seems worth the effort of creating a Script, ctrl-shift clicking the visibility icon and selecting "Hide all other layers" then "Delete Hidden Layers" from the Layers Panel’s fly-out menu would seem easy enough.

          hideAllOtherLayers.jpg

          • 2. Re: Remove not active layer
            vishok75 Level 1

            ues but beacuse i have a lot of images i would like to do this with a script .jsx

            • 3. Re: Remove not active layer
              c.pfaffenbichler Level 9

              You should be able to use ScriptingListener.plugin to record the aforementioned steps, actually even an Action should work.

              Sorry, did a test, this is apparently not the case.

              • 4. Re: Remove not active layer
                c.pfaffenbichler Level 9

                Sorry, my mistake, "Hide all other layers" does not seem to record properly.

                • 5. Re: Remove not active layer
                  c.pfaffenbichler Level 9

                  Would it be acceptable to leave Groups alone?

                  Because while one can fairly easily identify the "selected Layers" the Groups they are contained in are harder to identify.

                  • 6. Re: Remove not active layer
                    vishok75 Level 1

                    yes i have only layer not grouped. example.jpg

                    • 7. Re: Remove not active layer
                      c.pfaffenbichler Level 9

                      Could you give this a try?

                      // based on code by paul mr; removes unselected layers but leaves layersets untouched;

                      // 2014, use at your own risk;

                      #target "photoshop-70.032"

                      if (app.documents.length > 0) {

                      try {activeDocument.selection.deselect()} catch (e) {};

                      // get selected layers’ indices;

                      var theSelected = getSelectedLayersIdx();

                      // select the previously unselected layers;

                      selectUnselected (theSelected);

                      // delete;

                      // =======================================================

                      var idDlt = charIDToTypeID( "Dlt " );

                          var desc3 = new ActionDescriptor();

                          var idnull = charIDToTypeID( "null" );

                              var ref2 = new ActionReference();

                              var idLyr = charIDToTypeID( "Lyr " );

                              var idOrdn = charIDToTypeID( "Ordn" );

                              var idTrgt = charIDToTypeID( "Trgt" );

                              ref2.putEnumerated( idLyr, idOrdn, idTrgt );

                          desc3.putReference( idnull, ref2 );

                      executeAction( idDlt, desc3, DialogModes.NO );

                      };

                      /*//////

                      function GetSelectedLayers() {

                      var A=[];

                          var desc11 = new ActionDescriptor();

                              var ref9 = new ActionReference();

                              ref9.putClass( stringIDToTypeID('layerSection') );

                          desc11.putReference( charIDToTypeID('null'), ref9 );

                              var ref10 = new ActionReference();

                              ref10.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );

                          desc11.putReference( charIDToTypeID('From'), ref10 );

                          executeAction( charIDToTypeID('Mk  '), desc11, DialogModes.NO );

                      var gL = activeDocument.activeLayer.layers;

                      for(var i=0;i<gL.length;i++){

                      A.push(gL[i]);

                      }

                      executeAction( charIDToTypeID('undo'), undefined, DialogModes.NO );

                      return A;

                      };

                      */

                      ////////////////////////////////////

                      function selectUnselected (theSelected) {

                      // check for background layer;

                      var ref = new ActionReference();

                      ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

                      var docDesc = executeActionGet(ref);

                      var hasBackground = docDesc.getBoolean(stringIDToTypeID("hasBackgroundLayer"));

                      // get number of layers;

                      var ref = new ActionReference();

                      ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

                      var applicationDesc = executeActionGet(ref);

                      var theNumber = applicationDesc.getInteger(stringIDToTypeID("numberOfLayers"));

                      //////;

                      if (hasBackground == false) {var theStart = 1} else {var theStart = 0};

                      for (var m = 0; m <= theNumber; m++) {

                      try {

                      var ref = new ActionReference();

                      //ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

                      ref.putIndex( charIDToTypeID( "Lyr " ), m);

                      var layerDesc = executeActionGet(ref);

                      var layerSet = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("layerSection")));

                      var isBackground = layerDesc.getBoolean(stringIDToTypeID("background"));

                      // if not layer group collect values;

                      if (layerSet != "layerSectionEnd" && layerSet != "layerSectionStart") {

                      var theCheck = false;

                      for (var n = 0; n < theSelected.length; n++) {

                      if (theSelected[n] == m) {theCheck = true}

                      };

                      if (theCheck == false) {

                      if (m == theStart) {selectLayerByIndex(m,false)}

                      else {selectLayerByIndex(m,true)};

                      };

                      };

                      }

                      catch (e) {};

                      };

                      };

                      ////// by paul mr;

                      function getSelectedLayersIdx(){

                            var selectedLayers = new Array;

                            var ref = new ActionReference();

                            ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

                            var desc = executeActionGet(ref);

                            if( desc.hasKey( stringIDToTypeID( 'targetLayers' ) ) ){

                               desc = desc.getList( stringIDToTypeID( 'targetLayers' ));

                                var c = desc.count

                                var selectedLayers = new Array();

                                for(var i=0;i<c;i++){

                                  try{

                                     activeDocument.backgroundLayer;

                                     selectedLayers.push(  desc.getReference( i ).getIndex() );

                                  }catch(e){

                                     selectedLayers.push(  desc.getReference( i ).getIndex()+1 );

                                  }

                                }

                             }else{

                               var ref = new ActionReference();

                               ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "ItmI" ));

                               ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

                               try{

                                  activeDocument.backgroundLayer;

                                  selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" ))-1);

                               }catch(e){

                                  selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" )));

                               }

                            }

                            return selectedLayers;

                      };

                      // by mike hale, via paul riggott;

                      // http://forums.adobe.com/message/1944754#1944754

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

                      }

                      };

                      • 8. Re: Remove not active layer
                        vishok75 Level 1

                        I get an error :

                         

                        Linea: 95

                        ->  var hasBackground = docDesc.getBoolean(stringIDToTypeID("hasBackgroundLayer"));

                        • 9. Re: Remove not active layer
                          c.pfaffenbichler Level 9

                          Which version of Photoshop are you using?

                          • 10. Re: Remove not active layer
                            vishok75 Level 1

                            photoshop cc

                            Version 14.0 x64

                            • 11. Re: Remove not active layer
                              c.pfaffenbichler Level 9

                              That’s strange, it works fine for me in CC; but you may want to update Photoshop CC, though – it’s at 14.2.1 is the current version.

                               

                              You can try to replace that line with

                              var hasBackground = activeDocument.layers[activeDocument.layers.length - 1].isBackgroundLayer;

                              • 12. Re: Remove not active layer
                                vishok75 Level 1

                                you are great. works. Thank you