Skip navigation
Currently Being Moderated

match sublayer by name

May 7, 2012 1:26 PM

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");
 
 
 
Replies
  • Currently Being Moderated
    May 7, 2012 2:04 PM   in reply to hilukasz
    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
     
    
     
    |
    Mark as:
  • Currently Being Moderated
    May 7, 2012 2:13 PM   in reply to hilukasz

    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++) {
    

     
    |
    Mark as:
  • Currently Being Moderated
    May 7, 2012 2:32 PM   in reply to Mr Clint Eastwood

    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

     
    |
    Mark as:
  • Currently Being Moderated
    May 7, 2012 2:36 PM   in reply to Muppet Mark

    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…?

     
    |
    Mark as:
  • Currently Being Moderated
    May 7, 2012 2:52 PM   in reply to Muppet Mark

    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"

     
    |
    Mark as:
  • Currently Being Moderated
    May 7, 2012 2:57 PM   in reply to CarlosCanto

    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…

     
    |
    Mark as:
  • Currently Being Moderated
    May 8, 2012 5:44 AM   in reply to hilukasz

    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…

     
    |
    Mark as:
  • Currently Being Moderated
    May 8, 2012 7:53 AM   in reply to hilukasz

    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…

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points