Skip navigation
maphiom
Currently Being Moderated

Duplicate grouped objects in a different document

Nov 28, 2011 10:18 AM

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;

app.activeWindow.activePage.groups.add(myGroup1);

 

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

 

Ma

  • Currently Being Moderated
    Community Member
    Nov 28, 2011 12:59 PM

    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):

     

        app.activeWindow.activePage.groups.add(myGroup1);

     

    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];
     
    sourceLayer.pageItems.everyItem().duplicate(destLayer);
    
    |
    Mark as:
  • Currently Being Moderated
    Community Member
    Nov 29, 2011 4:26 PM

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

     

    Good question! I don't know

     

    @+

    Marc

    |
    Mark as:
  • Currently Being Moderated
    Community Member
    Nov 30, 2011 2:27 AM

    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])

    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points