Skip to main content
Participant
July 31, 2014
Answered

Duplicate object until artboard boundary is reached

  • July 31, 2014
  • 2 replies
  • 875 views

Hi Everyone!

I am new to JavaScript (4 days studying) and I am trying to make a object duplicate until artboard limit is touched .

Is this a correct way of doing it?

---

if (app.selection[0] != null) {

  var myDoc = app.activeDocument,

  selectedArt = myDoc.selection[0],

  limit = ?, // set number of duplicates  ???? how to stop when artboard limit is reached ?????

  horOffset = app.activeDocument.pathItems[0].width+34.016,  // set horizontal offset: selection width+12mm

  myDuplicate,

  i = 0;

  for (i;i < limit; i++){

  myDuplicate = selectedArt.duplicate();

  myDuplicate.position = [selectedArt.position[0] + horOffset * (i + 1), selectedArt.position[1]];

  }

  }

else {

  alert("You must have the object you want to duplicate selected.");

  }

---

I pieced this code together from other scripts,  however I got stuck at implementing the artboard limit.

I tried using limit = app.activeDocument.width, but its not working.

Let me know if the question makes sense.

Any advice is greatly appreciated. Thank you!

This topic has been closed for replies.
Correct answer liviuiancu

Its not pretty, but it works!

Not sure this will help anyone as the task is very specific.

I am using this to prepare labels for print on a paper roll. The artboard width is limited by the roll width.

The height limit is not important, as the setup document usually has only 2 rows.

Below is the code:

--------------

if (app.selection[0] != null) {

   

    //move object to start position

    app.activeDocument.pageItems[0].position = [28.347,-11.338]

   

    //Make duplicates horizontally

   

  var myDoc = app.activeDocument,

        selectedArt = myDoc.selection[0],

  limit = Math.floor ((app.activeDocument.width-56.688)/(app.activeDocument.selection[0].width+34.016)-1) ,

  horOffset = app.activeDocument.selection[0].width+34.016,  // set horizontal offset: selection width+12mm

  myDuplicate,

  i = 0;

  for (i;i < limit; i++){

  myDuplicate = selectedArt.duplicate();

  myDuplicate.position = [selectedArt.position[0] + horOffset * (i + 1), selectedArt.position[1]];

        

        }

  }

// Duplicates one row down

if ( app.documents.length > 0 ) {

docSelection = app.activeDocument.selection;

if ( docSelection.length > 0 ) {

newGroup = app.activeDocument.groupItems.add();

for ( i = 0; i < docSelection.length; i++ ) {

newItem = docSelection.duplicate();

newItem.moveToBeginning( newGroup );

newItem.top = newItem.top - newItem.height - 17.008;

}

}

}

else {

  alert("You must have the object you want to duplicate selected.");

  }

The funny thing is I am not sure why the duplication of one row down works

Also if the art is on the wrong layer, the object will not be moved to the correct start point.

2 replies

Sergey_Anosov
Participating Frequently
August 1, 2014

Hi!

I really doubt it will be a correct answer, but hope it will be helpful:

https://sites.google.com/site/dtpscripting/illustrator-scripts/stepandcopyai

Participant
August 1, 2014

Sergey, thanks for the link! Even if its not what I am looking for, its a very helpful example.

Thanks!

CarlosCanto
Community Expert
Community Expert
August 1, 2014

you're welcome, don't get discouraged, you're doing good so far. The documentation is scarce and not very intuitive but little by little you'll get the hang of it, that's part of the fun too, I'm 3 years into this and still learning.

CarlosCanto
Community Expert
Community Expert
July 31, 2014

use artboardRect to get the Artboard bounds, it would be an array (left, top, right, bottom)

then instead of a for loop, use a while loop to check if the selection width+offset < array[2]

Participant
August 1, 2014

Hi Carlos!

Thanks for the advice. I will return with the code once I get it to work.

For now, after a week of study, I am struggling to understand how commands come together and what works.

Thank you!