4 Replies Latest reply on Nov 30, 2011 1:27 AM by [Jongware]

    Duplicate grouped objects in a different document

    maphiom Level 1

      Hi guys,


      I am writing a script to copy parts of a document into another document. In document A, I have isolated each group of elements (about two dozen text and graphic frames in each case) onto separate layers. So I want the script to group all objects on a given layer and then duplicate them in document B, at a specific position.


      So far, I have this :


      var myLayer1 = app.documents.item(0).layers.item("Layer1");

      var myObjects1 = myLayer1.pageItems.everyItem();


      var myGroup1 = new Array;

      myGroup1 = myObjects1;



      myGroup1.duplicate([-120, 0]);


      I just tested the script by trying to duplicate my group of objects in another part of the same document, but this doesn't preserve each object's coordinates inside the group, instead giving all the duplicated objects the same coordinates [-120, 0]. The result is all my objects piled up, aligned by their top left corner. I might be missing something obvious. How can I preserve my objects' relative positions?


      Thanks a lot



        • 1. Re: Duplicate grouped objects in a different document
          Marc Autret Level 4

          Hi ma,


          In your code, myGroup1 is never a group. It is just a copy of myObjects1, due to:


              myGroup1 = myObjects1;


          where myObjects1 is a plural PageItem, due to:


              var myObjects1 = myLayer1.pageItems.everyItem();


          So myGroup1.duplicate() only duplicates a plural PageItem.


          Meanwhile, the interesting thing is that you create a group (although I'm not sure how it really works, since I thought groups.add were only supporting as argument an Array of items—and you don't supply an array):




          Then, since you don't store the result of groups.add() in a variable, you don't have access to that group. Thus, in fact, you just duplicate the page items separately and move each of them to the absolute coordinates [-120,0] (the first argument of the duplicate method is an absolute location).


          Note that another possible issue is that groups.add() could fail if the page items of Layer1 don't all rely on a same Spread, because you cannot create a group from multiple spread items.


          Now, one question: are you sure you need to create a group? If you only want to duplicate a set of items from doc1 to doc2, why don't you directly use duplicate? Here is a basic example:



          var sourceLayer = app.documents[0].layers.itemByName("Layer1");
          var destLayer = app.documents[1].layers[0];
          • 2. Re: Duplicate grouped objects in a different document
            maphiom Level 1

            Hey Marc,


            thanks a lot! You're right, I didn't need to group my set of items. I just believed I needed to do it in order to keep my items' positions.


            I just have one question left :

            The way my script works, I need to duplicate the items in another document (that works fine thanks to your help), and at a precise location on the page. But if I use the following line :


            myLayer1.pageItems.everyItem().duplicate(destLayer,[119.5, 164]);


            The set of coordinates is interpreted as the "by" parameter, thus offsetting my items from their original position. Is there a way to specify the target document AND the target absolute coordinates in the  "to" parameter?


            Sorry if the answer is obvious



            • 3. Re: Duplicate grouped objects in a different document
              Marc Autret Level 4

              > Is there a way to specify the target [layer] AND the target absolute coordinates?


              Good question! I don't know




              • 4. Re: Duplicate grouped objects in a different document
                [Jongware] Most Valuable Participant

                The first "to" argument of duplicate dubs for either an exact location on the same page or 'any other place' on another page or layer:


                PageItem duplicate ([to: {Array of 2 Units | Spread | Page | Layer} ][, by: Array of Measurement Unit (Number or String)=any])