2 Replies Latest reply on Mar 11, 2010 1:33 AM by Peter Kahrel

    How to add a pagitems to a group

    FlexMitchell Level 1

      mygroup := Application.ActiveDocumnet.Groups,Add(),

      then I can not find any method can add in InDesign CS4 Group object?

      Anybody show me a hint , please;

      Thanks

        • 1. Re: How to add a pagitems to a group
          Kasyan Servetsky Level 5

          This is what I found in my archive. Don't remember who is the author of the code — probably Marc Autret.

           

          Group.prototype.addItems = function(/*PageItem[] | PageItem*/ newItems)
          //----------------------------
          // Emulates Group.groups.add(items) or Group.add(existing_item)
          // <newItems> == Array of 2+ PageItem -> create a <newItems> subgroup in <this>
          // <newItems> == single PageItem -> add the PageItem in <this>
          // Returns the added subgroup or item (the whole group hierarchy is preserved)
          // [this method fails if <this> is anchored/embedded in a story]
          {
          var     nodes=[], gs=[], g=this, node;
           
          var makeNode = function(id,g)
               {
               var elems = g.pageItems.everyItem().getElements();
               if (id === null) // placeholder for newItems
                    return {index: elems.length, items:elems.concat(0)};
               for(var i=elems.length-1 ; i>=0 ; i--)
                         // need to find the actual index in the group
                    if (elems[i].id == id) break;
               return {index: i, items:elems};
               }
           
          for( var id=null ; g.constructor == Group ; id=g.id, g=g.parent )
               {
               gs.push(g.getElements()[0]);
               nodes.push(makeNode(id,g));
               }
           
          var add = (function()
               {
               var host = g;
               return function(items){return host.groups.add(items);};
               })();
           
          var r = (typeof newItems == 'array' ) ?
               add(newItems) :
               newItems;
           
          while( g=gs.pop() ) g.ungroup();
           
          for( g=r ; node=nodes.shift() ; )
               {
               node.items[node.index] = g;
               g = add(node.items);
               }
          return r;
          }
          
          //-----------------------------------------------------------------------
          var myDoc = app.activeDocument;
          var myGroup = myDoc.groups[0];
          
          // Sample usages:
          var mySingleTextFrame = myDoc.textFrames[0];
          // or
          //~ var subGroup = myGroup.addItems(myNewTFs);
          
          myGroup.addItems(mySingleTextFrame);
          
          • 2. Re: How to add a pagitems to a group
            Peter Kahrel Adobe Community Professional & MVP

            > probably Marc Autret.

             

            Correct: http://forums.adobe.com/message/2259499#2259499

             

            Peter

            1 person found this helpful