10 Replies Latest reply on Mar 31, 2016 3:38 AM by Marc Autret

    Layer hierarchy

    tpk1982 Level 4



      I need to check my layer order names. The bottom two layers should be Layer 1 and Layer 2. And the top most layer is Layer 3. The other layers in between Layer 2 and Layer 3 maybe different count. (not fixed, it maybe any name, we don't bother this). I tried with negative for loop but not sure how to go ahead. Please help me.


      Screen Shot 2016-03-29 at 5.52.40 PM.png


      Tried coding:


      var myDoc = app.activeDocument;  
      var myLayers = myDoc.layers;  
      for(i = myLayers.length; 0<i; i--) {  
      //alert("Layer names of active document:"+"\r"+app.activeDocument.layers.everyItem().name.join("\r"));    



      Warm regards,


        • 1. Re: Layer hierarchy
          Loic.Aigon Adobe Community Professional

          What are you looking for exactly ?

          • 2. Re: Layer hierarchy
            tpk1982 Level 4

            Hi Loic,


            I need to verify the below pattern of Layers, in the layer palette.

            1. The top most layer should be Layer 3

            2. The two bottom most layers should be Laye r2 and Layer 1

            3. The layers in between Layer 3 and Layer 2 does not matter. ( in my sample screen shot, it have layers 1, 2, 3 and xxxx.)

            If the above pattern does not match in the layer palette, then alert message to be displayed.



            • 3. Re: Layer hierarchy
              Ronald63 Level 4



              Try this ...

              var _LayersName = app.activeDocument.layers.everyItem().name;
              if (_LayersName.shift() != 'Layer 3' || _LayersName.pop() != 'Layer 1' || _LayersName.pop() != 'Layer 2'){
                  alert('alert message')



              • 4. Re: Layer hierarchy
                Laubender Adobe Community Professional & MVP

                Hi Karthi,

                the top most layer is always of index 0. Be it a collection, be it an array.

                The bottom most is always the last one in the stack. In a collection: -1 or length of collection -1 , in an array: length of array -1.

                Therefore you also search for this: In a collection: -2 or length of collection -2 , in an array: length of array -2.


                This should be enough to start a check on the layers hierarchy.


                var doc = app.documents[0];
                var layerNames = doc.layers.everyItem().name;
                var layerNamesLength = layerNames.length;
                // Test for a postive:
                    layerNamesLength >= 3  // We need a minimum of three layers
                    layerNames[0] == "Layer 3" // The first layer of the stack
                    layerNames[layerNames.length-2] == "Layer 2" // The second after the bottom most
                    layerNames[layerNames.length-1] == "Layer 1" // The bottom most
                    // do nothing.
                    // One simple alert says really not much:
                    alert("Not the order we want.");
                    // Some more testing?
                    // A more detailed alert?


                Should the layers be moved in the right order, if they are present?

                Should new layers be added at the right position in the stack, if they are missing?

                Then we could write more intelligent code…



                • 5. Re: Layer hierarchy
                  Peter Kahrel Adobe Community Professional & MVP

                  Why not put those layers in their place it they're present:


                  layers = app.documents[0].layers;
                  if (layers.item('Layer 2').isValid) {
                    layers.item('Layer 2').move (LocationOptions.AT_END);
                  if (layers.item('Layer 1').isValid) {
                    layers.item('Layer 1').move (LocationOptions.AT_END);
                  if (layers.item('Layer 3').isValid) {
                    layers.item('Layer 3').move (LocationOptions.AT_BEGINNING);



                  • 6. Re: Layer hierarchy
                    tpk1982 Level 4

                    Hi Roland,


                    You coding simple and this is what i need exactly. Thanks you so much.


                    @Uwe: Again you explained me the coding with logic what i should think. Really appreciated . Immense THANKS.


                    @Peter: Thank you so much Peter. Your coding fixes if it is not in the correct order. Really helpful. But sometimes client don't like we fix the issue. They want only error report.


                    Once again thanks all for your valuable inputs.




                    • 7. Re: Layer hierarchy
                      Marc Autret Level 4

                      Just for fun:


                      var r = /^Layer 3,.+,Layer 2,Layer 1$/.test(app.activeDocument.layers.everyItem().name.join(','));
                      alert( r ? 'OK' : 'KO' );




                      • 8. Re: Layer hierarchy
                        tpk1982 Level 4

                        OMG..      AWESOME MARC..


                        I always amazed with you codings.. Eager to know how to learn this kind of using logics..


                        Basically i am a Mechanical Engineering graduate.. with my self interest i learnt Javascript from Forum and Adobe provided PDFs..




                        • 9. Re: Layer hierarchy
                          Harbs. Level 6

                          It took me a minute to figure out what you did there, Marc.


                          The JavaScript syntax formating on the forum obviously does not do a good job with RegEx literals.


                          For anyone struggling with what Marc wrote, here's a breakdown:


                          1. /^Layer 3,.+,Layer 2,Layer 1$/ is a Regular Expression literal -- the same as new RexExp("^Layer 3,.+,Layer 2,Layer 1$").
                            1. The "^" means it;s the start of the string.
                            2. The "$" means the end of the string.
                            3. So, the regex matches a string which starts with "Layer 3," has some other text afterwards, continues with "Layer 2," and ends with "Layer 1".
                          2. RegExp has a method called "test" which check that it matches some text.
                          3. app.activeDocument.layers.everyItem().name gives an array of every layer name in order.
                          4. Array.join(",") builds a string with commas between the names.
                          5. The second line checks whether the test passes and alerts either OK or KO (if false).


                          Marc, I'm disappointed. You could have made it even more terse by skipping the "r" variable...



                          • 10. Re: Layer hierarchy
                            Marc Autret Level 4

                            Harbs. wrote:


                            Marc, I'm disappointed. You could have made it even more terse by skipping the "r" variable...