4 Replies Latest reply on May 6, 2013 3:50 PM by Michael L Hale

    Determine if hidden layers exist

    MarkWalsh Level 4

      I am building a script to cleanse photoshop documents, and one of the options I am including is to remove all hidden layers. There is no direct command to do so (or none that I have seen), so I used a chunk of code from ScriptListener to perform the 'Delete hidden layers' command.


      I only want to enable this option if there are layers that are hidden, so I'm wondering if there is a simple way to determine if any layers are hidden in the document (aside from looping through all of the layers). Or, if there's a way to determine if the 'Delete hidden layers' command is enabled or not, since that is only enabled when there are hidden layers.

        • 1. Re: Determine if hidden layers exist
          Paul Riggott Level 6

          I would just bung the code between a try catch block (but I'm lazy )

          If you did want to check this should be fast...



          function hasHiddenLayers(){ 
             var ref = new ActionReference(); 
             ref.putEnumerated( charIDToTypeID('Dcmn'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') ); 
             var count = executeActionGet(ref).getInteger(charIDToTypeID('NmbL')) +1; 
          var i = 0; }catch(e){ var i = 1; };
                 if(i == 0) continue;
                  ref = new ActionReference(); 
                  ref.putIndex( charIDToTypeID( 'Lyr ' ), i );
                  var desc = executeActionGet(ref);
                  var layerName = desc.getString(charIDToTypeID( 'Nm  ' ));
                  var Id = desc.getInteger(stringIDToTypeID( 'layerID' ));
                  if(layerName.match(/^<\/Layer group/) ) continue;
                  if(!desc.getBoolean(charIDToTypeID( "Vsbl" ))) return true;
          return false;
          • 2. Re: Determine if hidden layers exist
            Michael L Hale Level 5

            What version of Photoshop are you using? Newer version do have a 'delete hidden layers' command that you could call using app.runMenuItem()

            1 person found this helpful
            • 3. Re: Determine if hidden layers exist
              MarkWalsh Level 4

              Thanks, Paul, that seems to work. Haven't tried it against any massive files yet, but it works with the files I tested.



              Michael, I am using CS5-CS6. The 'Delete hidden layers' command was what I recorded with the ScriptListener, would have used the runMenuItem, but I'm lazy like Paul, and didn't bother to figure out how to look up the index.


              Thanks to you both

              • 4. Re: Determine if hidden layers exist
                Michael L Hale Level 5

                I miss-read the post. Yeah I agree with Paul - just warp the executeAction in a try/catch.


                I tried to see if I could get a descriptor for hidden layers with this code.


                var desc = new ActionDescriptor();
                    var ref = new ActionReference();
                    ref.putEnumerated( charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), stringIDToTypeID( "hidden" ) );
                var desc = executeActionGet(ref)

                But it returns the descriptor for the top layer regardless of that layer's visibility.