Skip to main content
Participating Frequently
February 3, 2015
Answered

duplicating multiple groups?

  • February 3, 2015
  • 1 reply
  • 683 views

Hello all,

I am attempting to develop a script where i have a selection of a group and it would duplicate twice along the x axis.  Then, take all the grouped items and duplicate it down the y axis 3x. 
This is what i have so far:

var myDoc = app.activeDocument;

var myNeckLabel = myDoc.selection[0];

// For loop duplicating selection, moving to the left 2x

for (var sel=0; sel<[2]; sel++){

  myNeckLabel.duplicate();

  myNeckLabel.translate(-3.9327*72);

}

// For loop duplicate selection down 3x

for (var sel=0; sel<[3]; sel++){

  myNeckLabel.duplicate();

  myNeckLabel.translate(0,-3.153*72);

}

What the above produced for me is the desired effect, except it will only do it down the left column, so i would have the top row and the left column filled out. how do i select every object the first loop creates and then feed it into the second loop?
Thanks for the help in advance !

This topic has been closed for replies.
Correct answer Qwertyfly___

Does this do what you want?

var doc = app.activeDocument; 

var sel = doc.selection; 

dup(sel,0,2,-3.9327*72,0);

sel = doc.selection;

for(var i = 0; i < sel.length; i++) 

dup(sel,i,3,0,-3.153*72);

}

sel = doc.selection;

var totalMatrix = app.getScaleMatrix(-100,100);

for(var j = 0; j <sel.length; j++) 

sel.transform(totalMatrix);

}

function dup(sel,item,qty,tran1,tran2)

{

for (var i=0; i<[qty]; i++){ 

  sel[item].duplicate(); 

  sel[item].translate(tran1,tran2); 

}

1 reply

Qwertyfly___
Legend
February 4, 2015

my solution was to group the top row then continue

var myDoc = app.activeDocument;

var myNeckLabel = myDoc.selection[0];

// For loop duplicating selection, moving to the left 2x

for (var sel=0; sel<[2]; sel++){

  myNeckLabel.duplicate();

  myNeckLabel.translate(-3.9327*72);

}

var newSelection = myDoc.selection;

var newGroup = myDoc.groupItems.add();

for(var i = 0; i < newSelection.length; i++)

{

    newSelection.move(newGroup,ElementPlacement.INSIDE);

}

// For loop duplicate selection down 3x

for (var sel=0; sel<[3]; sel++){

  newGroup.duplicate();

  newGroup.translate(0,-3.153*72);

}

Participating Frequently
February 4, 2015

Thanks imagecollection ! putting into a group worked.
this is what i ended up doing with the code:

var myDoc = app.activeDocument;

var myNeckLabel = myDoc.selection[0];

// For loop duplicating selection, moving to the left 2x

for (var sel=0; sel<[2]; sel++){

  myNeckLabel.duplicate();

  myNeckLabel.translate(-3.9327*72);

}

var newSelection = myDoc.selection;

var newGroup = myDoc.groupItems.add();

for(var i = 0; i < newSelection.length; i++)

{

    newSelection.move(newGroup,ElementPlacement.INSIDE);

}

// For loop duplicate selection down 3x

for (var sel=0; sel<[3]; sel++){

  newGroup.duplicate();

  newGroup.translate(0,-3.153*72);

}

var totalMatrix = app.getScaleMatrix(-100,100);

var allSelected = myDoc.selection;

var wholeGroup = myDoc.groupItems.add()

for (var i = 0; i < allSelected.length;i++)

{

    allSelected.move(wholeGroup,ElementPlacement.INSIDE);

}

app.activeDocument.activeLayer.pageItems[0].transform(totalMatrix);

By chance do you know what the best way for me to do this without grouping everything into one massive object?

I tried aciveDocument.selectObjectsOnActiveArtboard(true);  but it didn't work out.

Thanks again for the help

Qwertyfly___
Qwertyfly___Correct answer
Legend
February 5, 2015

Does this do what you want?

var doc = app.activeDocument; 

var sel = doc.selection; 

dup(sel,0,2,-3.9327*72,0);

sel = doc.selection;

for(var i = 0; i < sel.length; i++) 

dup(sel,i,3,0,-3.153*72);

}

sel = doc.selection;

var totalMatrix = app.getScaleMatrix(-100,100);

for(var j = 0; j <sel.length; j++) 

sel.transform(totalMatrix);

}

function dup(sel,item,qty,tran1,tran2)

{

for (var i=0; i<[qty]; i++){ 

  sel[item].duplicate(); 

  sel[item].translate(tran1,tran2); 

}