11 Replies Latest reply: May 8, 2012 7:53 AM by Muppet Mark RSS

    match sublayer by name

    hilukasz Community Member

      I first want to match a top level layer by name then find all group items, but only group items that are under the the "my layer" name and leave the rest of the groups aline. I need to first iterate through the top layers, then through the groupItems, however I cannot seem to access group items as children of layers. here is how I thought it would work.

       

      function findGroups(layerName) {
          for (var i = 0; i < numberOfLayers; i++) { 
              var currentLayer = app.activeDocument.layers[i].name;
              if (currentLayer == layerName) {
                  var numberOfSublayers = app.activeDocument.layers[i].groupItems.length;
                  for (var i = 0; i < numberOfSublayers; i++) { 
                      // do something
                  }   
              }
          }
      }
      
      
      findGroups("my layer");
      
      
      
        • 1. Re: match sublayer by name
          Mr Clint Eastwood Community Member
          function findGroups(layerName) {
            var numberOfLayers = app.activeDocument.layers.length; // numberOfLayers was not defined
              for (var i = 0; i < numberOfLayers; i++) {
                  var currentLayer = app.activeDocument.layers[i].name;
                  if (currentLayer == layerName) {
                      var numberOfSublayers = app.activeDocument.layers[i].layers.length; // Number of sublayers
                      for (var x = 0; x < numberOfSublayers; x++) { // Dont use the i variable twice in the same function. It could work but not best practice.
                          alert(app.activeDocument.layers[i].layers[x]) // Alert the sublayers
                      }  
                  }
              }
          }
          
          
          findGroups("My Layer"); // Case Sensitive
          
          
          • 2. Re: match sublayer by name
            CarlosCanto MVP

            don't use i all the time in your for loops, change the second for i's to something else

             

            for (var j = 0; j < numberOfSublayers; j++) {

            • 3. Re: match sublayer by name
              Muppet Mark Community Member

              Oh where have they put the advanced editor? No code thing… Oh well never mind… This should suffice… getByName() should go in atry catch but you get the idea…

               

              #target illustrator
              
              var doc = app.activeDocument;
              
              var grps = doc.layers.getByName( 'my layer' ).groupItems;
              
              for ( var i = 0; i < grps.length; i++ ) {
              
                        doc.selection = null;
              
                        grps[i].selected = true;
              
                        app.redraw();
              
                        alert( 'group '+ ( i+1 ) );
              
              };
              

               

              See pic for difference between types…

               

              Screen shot 2012-05-07 at 22.29.44.png

              • 4. Re: match sublayer by name
                hilukasz Community Member

                @carlos, yikes that was a silly overlook on my part. you're totally right.

                • 5. Re: match sublayer by name
                  Muppet Mark Community Member

                  Am I having a bit of a thick moment or do I really have to post then go back and edit to get the advanced features…?

                  • 6. Re: match sublayer by name
                    CarlosCanto MVP

                    Am I having a bit of a thick moment or do I really have to post then go back and edit to get the advanced features…?

                     

                    hahaha, yes, that's part of the new forum "improvements"

                    • 7. Re: match sublayer by name
                      Muppet Mark Community Member

                      I see Jive is going the route of CS… take something useful away then call it an improvement when you put it back… Keep that in mind for your scripts…

                      • 8. Re: match sublayer by name
                        hilukasz Community Member

                        I got something similar to this working. however I don't want to get all the groupItems in a single top layer and ignore the other top layers

                         

                        ie:

                         

                        top layer

                             groupItem 1

                             groupItem 2

                        Another Top Layer

                             groupItem 3

                         

                        I want it only to match for the top layer groupItem1 and groupItem2.

                        • 9. Re: match sublayer by name
                          Muppet Mark Community Member

                          That is NOT what I get… Collections have scope to their parent container… When my snippet was run against the pictured doc it selected only the 4 groups in my Layer all other top layers had groups in them…

                          • 10. Re: match sublayer by name
                            hilukasz Community Member

                            no, you're totally right, I'm a dummy and had some extra code in my prototype that was selecting other groups. It works perfect and how you would expect. You're right, since other groups would be out of the parent group. Curious about the app.redraw bit and what it is doing. I have had to use this to fix bugs, is that the case here?

                            • 11. Re: match sublayer by name
                              Muppet Mark Community Member

                              app.redraw(); is just a script method that should cause the GUI window to update by default this does NOT happen for performace… So using it slows things down… I have had some occasions where it has not worked but a dialog will force that… It shouldn't fix anything but I wanted the highlighted selecion to be visible…