Skip navigation
msismsis
Currently Being Moderated

detect groups in pageitems and cycle through items in each group

Aug 14, 2011 6:31 AM

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.

 
Replies
  • Currently Being Moderated
    Aug 16, 2011 1:31 AM   in reply to msismsis

    Hi,
    if you have 3 pageItems and these are in same group.

     

    alert (app.activeDocument.pageItems.length);

     

    There are only 3 items in the Document but this one line scipt return 4(items).
    One is GroupeItem and others are page Items. (and origin pageItems parent is GroupItem.)
    So you should check and move only groupItems to target layer.

     

    if (app.activeDocument.pageItems[0].typename=="GroupItem") {
        app.activeDocument.pageItems[0]

             .moveToBeginning(app.activeDocument.layers[0])
        }

     

    You can move Items to specific layer using "moveToBeginnin()" method like prevoius script.

     

    Ten.

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 17, 2011 1:52 AM   in reply to msismsis

    Too many object... How about that deletes pathItems smaller than the defined size before you run it?

     

    Ten

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 17, 2011 1:42 PM   in reply to msismsis

    Have you tried running the loop backwards in case the index gets messed up.

     m=0; m < CurrentItem.pageItems.length-1; m-- 
     
    |
    Mark as:
  • Currently Being Moderated
    Aug 17, 2011 2:31 PM   in reply to msismsis

    if you have the following group

     

    "layer 1"

    - Group               // pageItem[0]

         - pathItem1     // pageItem[1]

         - pathItem2     // pageItem[2]

     

    and you move out pathItem[1] to your "dummy" layer, being the top most layer. you'll get this

    "dummy"

     

         - pathItem1     // pageItem[1]

    "layer 1"

         - Group               // pageItem[0]

              - pathItem2     // pageItem[2]

     

     

    ...so the staking order gets messed up, you'll have to either

    - put the pageItems to move in an array, and after your original for...next is done, loop thru the items in the array to move to the "dummy" layer

     

    or

    - loop backwards, and move your items to a layer at the bottom of the staking order

     

    "layer 1"

         - Group               // pageItem[0]

                   - pathItem1     // pageItem[1]

     

    "dummy"

         - pathItem2     // pageItem[2]

     

     

     

    Edit: Hi Larry, I took too long compose

     

    Message was edited by: CarlosCanto

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 17, 2011 5:07 PM   in reply to msismsis

    Good morning. I examined this code last night and found a problem.
    When you call your function, global variable "j" is overrided by local variable. You must define variables to local in function like below:

     

    function LayerExists(FindLayer)
    {
      var layerCount = doc.layers.length;
      var FindIt = 0; 
      for (
    var j=0; j < layerCount; j++ )
       {
        if ( doc.layers[j].name == FindLayer ) { FindIt = 1; };
       }
      return FindIt;
    }


    Ten

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 18, 2011 8:49 AM   in reply to msismsis

    And how about simply replace LayerExists and ConstructLayerNameBasedOnFillColor two functions with

     

    function ConstructLayerNameBasedOnFillColor (/*arguments*/) {
         var myLayerName = "dummy";
         try {
              doc.layers.getByName(myLayerName);
         } catch (e) {
              doc.layers.add().name = myLayerName;
         }
         return myLayerName ;      
    }
    

     
    |
    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