4 Replies Latest reply on Dec 16, 2015 2:46 AM by cbuliarca

    How to traverse all layers?

    MrDesign Level 1

      I would like to write a script to rename all layers, but I don't know how to do it, who can help me?

        • 1. Re: How to traverse all layers?
          oliverIntergrafika Level 2
          function traverse( layerSet, callback )
          {
            for ( var i = 0; i < layerSet.layers.length; i++ ) {
                 var l = layerSet.layers[ i ];
                 app.activeDocument.activeLayer = l;
                 if ( l.typename == "LayerSet" )
                 {
                      traverse( l, callback );
                 }
                 callback( l );
            }
          }
          
          function toUpp( l )
          {
              l.name = l.name.toUpperCase();
          }
          
          traverse( app.activeDocument, toUpp );
          
          
          

           

          In this case the callback is setting the layer name to uppercase. The app.activeDocument is a layerSet too. For some reason you have to select one layer before running this script.

          • 2. Re: How to traverse all layers?
            MrDesign Level 1

            Your script can really do this, but it is running very slowly in many layers of the PSD file, do you have a way to optimize it?

            • 3. Re: How to traverse all layers?
              oliverIntergrafika Level 2

              The PS DOM is ver slow. Sadly PS is rebuilding the whole Gaudi's Sagrada Familia from the ground up on every processor tick. There is no way to turn it off.

               

              Try to comment out line 5. The layers will flicker but, maybe it will be a bit faster.

               

              Or put layerSet.layers.length into a variable to prevent it's evaluation on every cycle.

              • 4. Re: How to traverse all layers?
                cbuliarca Level 3

                traverseAllLayers();

                function traverseAllLayers(){

                  xx = false;

                  var ref0 = new ActionReference();

                  ref0.putProperty( charIDToTypeID( 'Prpr' ), stringIDToTypeID('numberOfLayers') );

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

                  var desc0 = executeActionGet(ref0);

                  var i = desc0.getInteger(stringIDToTypeID('numberOfLayers'));

                  for(i; i > 0 ; i--){

                    ref = new ActionReference();

                    ref.putIndex( charIDToTypeID( 'Lyr ' ), i );

                    var desc = executeActionGet(ref);

                    var layerName = desc.getString(charIDToTypeID( 'Nm  ' ));

                    var Id = desc.getInteger(stringIDToTypeID( 'layerID' ));

                    var ls = desc.getEnumerationValue(stringIDToTypeID("layerSection"));

                    ls = typeIDToStringID(ls);

                    var vis = desc.getInteger(stringIDToTypeID( 'visible' ));

                    //---------------------------------------------------------------

                    alert(layerName);

                    //---put here what you want to do

                 

                 

                    //---------------------------------------------------------------

                 

                 

                  }

                }