Skip to main content
Known Participant
November 28, 2011
Answered

Duplicate grouped objects in a different document

  • November 28, 2011
  • 1 reply
  • 2616 views

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

This topic has been closed for replies.
Correct answer Marc Autret

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

1 reply

Marc Autret
Marc AutretCorrect answer
Legend
November 28, 2011

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

maphiomAuthor
Known Participant
November 29, 2011

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

Ma

Marc Autret
Legend
November 29, 2011

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

Good question! I don't know

@+

Marc