Quantcast
Channel: Adobe Community : Popular Discussions - Illustrator Scripting
Viewing all articles
Browse latest Browse all 12845

detect groups in pageitems and cycle through items in each group

$
0
0

Hi,

 

I can use some help with a script I'm working on. The script basically creates a new layer based on an objects fill color and then moves the item to the new layer.

 

What I can't figure out is how to check if a pageItem is a Group and if so, cycle through all objects within the group. What I have so far (snippit) is:

 

 

 

var doc = app.activeDocument;
var LayerName = "0";
var MyArray = new Array;

for ( j=0; j < Counter; j++ )    {    CurrentItem = doc.pageItems[j];    if (CurrentItem.typename != "GroupItem" && CurrentItem.typename != "CompoundPathItem" && CurrentItem.parent.typename != "GroupItem" && CurrentItem.parent.typename != "CompoundPathItem")       {         LayerName = ConstructLayerNameBasedOnFillColor( CurrentItem );        CurrentItem.move( app.activeDocument.layers.getByName( LayerName ), ElementPlacement.PLACEATBEGINNING );       }      else       {         if (CurrentItem.typename == "GroupItem") //  seems to be wrong as it also returns true for an item within the group        {         myArray = [];            for ................ // cycle through each item within the group             {              LayerName = ConstructLayerNameBasedOnFillColor( CurrentGroupItem );              MyArray.push(LayerName);             }         LayerName = GetMostCommonColor(MyArray);         ................. // move the group (including all items within the group) to layer LayerName          }       }    }

 

 

 

The part I need some help with is:

 

 

1:  if (CurrentItem.typename == "GroupItem") //  seems to be wrong as it also returns true for an item within the group
2:     {
3:     myArray = [];
4:      for ................ // cycle through each item within the group
5:         {
6:          LayerName = ConstructLayerNameBasedOnFillColor( CurrentGroupItem );
7:          MyArray.push(LayerName);
8:         }
9:     LayerName = GetMostCommonColor(MyArray);
10:     ................. // move the group (including all items within the group) to layer LayerName
11:    }

 

 

line 1: how to check if the pageItem is a group

line 4: cycle through all the items within the group

line 10: move the group to a new layer

 

Any help is appreciated.


Viewing all articles
Browse latest Browse all 12845

Trending Articles