Skip to main content
Known Participant
July 7, 2015
Question

Precise copy and move selected Objects using JavaScript to control Illustator CS6

  • July 7, 2015
  • 1 reply
  • 882 views

Sorry - I've asked this question before, but in the wrong forum!

I want to select everything in my document and copy it a precise

distance across the page (x number of times using a for loop)

and then everything down the page (y number of times, ditto)

for example:-

var stepac=58 //distance across

var stepback=349.25/6 //distance down

stepac=stepac*2.83463717

stepback=stepback*2.83463717

The measurements are in millimetres, then converted to points

for use with the correct syntax when I can discover what it is.

Any help given will be most gratefully received.

This topic has been closed for replies.

1 reply

Inspiring
July 7, 2015

This should be close enough to get you where you need to be. This is dynamic so each step is the size of the object selected. You can hard code your numbers in.

#target Illustrator

var idoc = app.activeDocument;

if (idoc.selection.length > 0){

  var sel = idoc.selection[0];

  var wide = sel.width;

  var wPlus = sel.width;

  var high = sel.height;

  var hPlus = sel.height;

  // i<5; can be edited to as many copies as you need;

  for (i=1; i<5; i++){

  var copyi = sel.duplicate();

  copyi.position = [copyi.position[0] + wide, copyi.position[1]];

  wide = wide + wPlus;

  redraw();

  }

  app.executeMenuCommand('group');

  sel = idoc.selection[0];

  for (j=1; j<5; j++){

  var copyj = sel.duplicate();

  copyj.position = [copyj.position[0], copyj.position[1] - high]

  high = high + hPlus;

  redraw();

  }

} else {

  alert("make a selection");

}

redraw();

Silly-V
Legend
July 7, 2015

Here is mine:

function StepAndRepeat(){var Units = {mm : 2.83464567,inch : 72,point: 1};function UIWindow(){function selectAll(elem){elem.active = false;elem.active = true;}var resultObj = {};var w = new Window("dialog", "Step and Repeat");var unitSelection = w.add('dropdownlist', undefined, Units.toSource().match(/(?!({|s))[a-z]+(?=(:))/g));if(typeof ___step_and_repeat_unitName == 'undefined'){unitSelection.selection = unitSelection.items[0];} else {unitSelection.selection = unitSelection.find(___step_and_repeat_unitName);}var p1 = w.add('panel', undefined, "Steps");p1.size = [120, 76];var p1_g1 = p1.add('group');var t = p1_g1.add('statictext', undefined, "Step X");var thisVal = (typeof ___step_and_repeat_stepX == 'undefined') ? 10 : ___step_and_repeat_stepX;var stepX = p1_g1.add('edittext', undefined, thisVal); stepX.characters = 5;t.addEventListener('mousedown', function(){selectAll(this.parent.children[1])});var p1_g2 = p1.add('group');var t = p1_g2.add('statictext', undefined, "Step Y");var thisVal = (typeof ___step_and_repeat_stepY == 'undefined') ? 10 : ___step_and_repeat_stepY;var stepY = p1_g2.add('edittext', undefined, thisVal); stepY.characters = 5;t.addEventListener('mousedown', function(){selectAll(this.parent.children[1])}); var p2 = w.add('panel', undefined, "Offsets");p2.size = [120, 76];var p2_g1 = p2.add('group');var t = p2_g1.add('statictext', undefined, "Offset X");var thisVal = (typeof ___step_and_repeat_offsetX == 'undefined') ? 1 : ___step_and_repeat_offsetX;var offsetX = p2_g1.add('edittext', undefined, thisVal); offsetX.characters = 5;t.addEventListener('mousedown', function(){selectAll(this.parent.children[1])});var p2_g2 = p2.add('group');var t = p2_g2.add('statictext', undefined, "Offset Y");var thisVal = (typeof ___step_and_repeat_offsetY == 'undefined') ? 1 : ___step_and_repeat_offsetY;var offsetY = p2_g2.add('edittext', undefined, thisVal); offsetY.characters = 5;t.addEventListener('mousedown', function(){selectAll(this.parent.children[1])});var btnOk = w.add('button', undefined, "OK");var btnCcl = w.add('button', undefined, "Cancel");if(w.show() == 2){alert("Cancelled");return null;} else {___step_and_repeat_unitName = resultObj.unitName = unitSelection.selection.text;resultObj.unitRatio = Units[unitSelection.selection.text];___step_and_repeat_stepX = resultObj.stepX = stepX.text;___step_and_repeat_stepY = resultObj.stepY = stepY.text;___step_and_repeat_offsetX = resultObj.offsetX = offsetX.text;___step_and_repeat_offsetY = resultObj.offsetY = offsetY.text;return resultObj;}}if(app.documents.length > 0){var doc = app.activeDocument;var sel = doc.selection;if(sel.length > 0){var userInput = UIWindow();try{if(userInput != null){var offsetX = userInput.offsetX * userInput.unitRatio;var offsetY = userInput.offsetY * userInput.unitRatio;var x = sel[0].position[0];var y = sel[0].position[1];var w = sel[0].width;var h = sel[0].height;for(var i = 0; i < userInput.stepY; i++){for(var j = 0; j < userInput.stepX; j++){var dupe = sel[0].duplicate(sel[0].parent, ElementPlacement.PLACEATBEGINNING);var xPos = (j == 0) ? x : x += (w + offsetX);dupe.position = [xPos, y];}y -= (h + offsetY);x = sel[0].position[0];}sel[0].remove();}} catch(e){alert(e);}} else {alert("Please make a selection first.");}}}

#target illustrator

StepAndRepeat();


And here is a more sophisticated & thought-out one, by someone else.

step_rep_inc.jsx

KorgpollyAuthor
Known Participant
July 13, 2015

I'm afraid neither of your scripts was simple enough

to enable me to get the gist of what I needed to do,

whilst elDudereno's was.

But thanks for your time and effort.  I really appreciate

you guys who take the time to post replies and help newbies

like me to get the hang of scripting.