Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A Script for Adobe Illustrator to Duplicate Items Hor/Ver with Margin
// A Script for Adobe Illustrator to Duplicate Items Hor/Ver with Margin
// Originally by https://github.com/hilukasz
// Modified by Mohamed ElShahawi (ExtremeGTX)
app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;
var doc = app.activeDocument;
var selected = doc.selection;
var selectedHeight = selected[0].height;
var selectedWidth = selected[0].width;
var OriginX = selected[0].position[0];
var OriginY = selected[0].position[1];
var repeatAmount,
myInput,
splitInput,
margin,
selectedPosition,
Direction;
// Run
userPrompt();
Direction == 'v' ? DuplicateVertical() : DuplicateHorizontal();
function DuplicateHorizontal(){
selectedPosition = selected.position;
var newItem = selected[0].duplicate( doc, ElementPlacement.PLACEATEND );
for(i=0; i< repeatAmount ; i++){
var newPosX = selectedWidth + newItem.position[0] + margin;
newItem.position = [newPosX,newItem.position[1]];
doc.selection = null;
newItem.selected = true;
var selectedPosition = selected.position;
newItem.duplicate( doc, ElementPlacement.PLACEATEND );
}
}
function DuplicateVertical(){
selectedPosition = selected.position;
var newItem = selected[0].duplicate( doc, ElementPlacement.PLACEATEND );
for(i=0; i< repeatAmount ; i++){
var newPosY = selectedHeight - newItem.position[1] + margin;
newItem.position = [newItem.position[0],-newPosY];
doc.selection = null;
newItem.selected = true;
var selectedPosition = selected.position;
newItem.duplicate( doc, ElementPlacement.PLACEATEND );
}
newItem.remove();
}
function userPrompt(){
myInput = prompt('Margin, Repeat Amount, Horizontal/Vertical','10,5,v');
splitInput = myInput.split(",");
margin = Number(splitInput[0]);
repeatAmount = Number(splitInput[1]);
Direction = splitInput[2].toLowerCase();
}
@siomosp

This comment has been minimized.

Copy link

@siomosp siomosp commented Nov 26, 2020

Hello,
it looks that script creates 2 more copies of the selection
12 instead of 10
And at the end of execution 2 shapes are selected
How i can fix it?
Regards

@ExtremeGTX

This comment has been minimized.

Copy link
Owner Author

@ExtremeGTX ExtremeGTX commented Nov 29, 2020

Hello,
it looks that script creates 2 more copies of the selection
12 instead of 10
And at the end of execution 2 shapes are selected
How i can fix it?
Regards

@SimoSP
Unfortunately, I don't have illustrator installed now, but make sure you are selecting only 1 object at time, may be there was another another object selected by mistake.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment