Hi!
So I have some Javascript I've compiled from sources and written myself to accomplish the task of duplicating an object across and down a page, along with adding a few elements at the end.
I must say it works pretty well and correctly, and I know this because it does so with SIMPLE vector artwork. Anything beyond a solid fill, single color background and Illustrator crashes.
I've tried this on numerous machines, from the ancient to the cutting-edge, CS5 to CC 2017, and the result is always the same....CRASH!
Can anyone help me in figuring out the cause of failure? Is it, perhaps, that I am trying to do too much with Javascript, or that my handle on the language is poor and inefficient?
Any help would be appreciated.
Code:
########################################################################################## ###########################################
//Variables established / The script will proceed if something is selected...
if (app.selection[0] != null) {
var myDoc = app.activeDocument,
selectedArt = myDoc.selection[0],
xclone = 4, // set number across
yclone = 6, // set number down
horOffset = 162, // set horizontal offset (pixels)
verOffset = -162, // set vertical offset (pixels)
myDuplicate,
i = 0
j = 0
//Duplicates the selection along the x axis
for (i;i < xclone;i++){
if (i === xclone) { break; }
myDuplicate = selectedArt.duplicate();
myDuplicate.position = [selectedArt.position[0] + horOffset * (i + 1), selectedArt.position[1] + verOffset * (0)];
}
//Groups all objects in the active document
for ( h = 0; h < myDoc.pageItems.length; h++ ) {
myDoc.pageItems[h].selected = true;
}
var docSelection = new Array;
docSelection = app.activeDocument.selection;
var Group1 = app.activeDocument.groupItems.add();
if (docSelection.length > 0) {
for (h = 0; h < docSelection.length; h++) {
docSelection[h].moveToEnd(Group1);
}
}
//Duplicates the selection (Group1) along the y axis
for (j;j < yclone;j++){
if (j === yclone) { break; }
myDuplicate = Group1.duplicate();
myDuplicate.position = [Group1.position[0] + horOffset * (0), Group1.position[1] + verOffset * (j + 1)];
}
//Groups all objects in the active document
for ( h = 0; h < myDoc.pageItems.length; h++ ) {
myDoc.pageItems[h].selected = true;
}
var docSelection = new Array;
docSelection = app.activeDocument.selection;
var Group2 = app.activeDocument.groupItems.add();
if (docSelection.length > 0) {
for (h = 0; h < docSelection.length; h++) {
docSelection[h].moveToEnd(Group2);
}
}
//...Otherwise an exception is thrown
}
else {
alert("You must have an object selected.");
}
{
//Resize Artboard [? , Height , Width , ?] (pixels)
app.activeDocument.artboards[0].artboardRect = [0,1134,871.2,0];
//Aligns selection (Group2) to artboard and deselects all
{
var myDoc = app.activeDocument;
app.coordinateSystem = CoordinateSystem.ARTBOARDCOORDINATESYSTEM;
var abIdx = myDoc.artboards.getActiveArtboardIndex();
var actAbBds = myDoc.artboards[abIdx].artboardRect;
var obj2move = Group2;
obj2move.position = new Array ((actAbBds[2]-actAbBds[0])/2 - obj2move.width/2, (actAbBds[3]-actAbBds[1])/2 + obj2move.height/2);
app.selection = null;
}
//Creates named paths to size and specifies a CMYK 100% Black fill [? , ? , Width , Height] (pixels)
{
var laserLine = app.activeDocument.pathItems.rectangle(0,0,9,1134);
laserLine.name = "laserLine";
var eyeMark = app.activeDocument.pathItems.rectangle(0,0,14.4,9);
eyeMark.position = [856.8,-1125]; //[X,Y] (pixels)
eyeMark.name = "eyeMark";
var k = new CMYKColor;
k.cyan= 0;
k.magenta = 0;
k.yellow = 0;
k.black = 100;
laserLine.fillColor = k;
eyeMark.fillColor = k;
}
}
{
//Variables established / selects eyeMark path and duplicates along the y axis
verOffset = 162, // set vertical offset (pixels)
yclone = 6, // set number up (offset is + now)
myDuplicate,
k = 0
app.activeDocument.pageItems.getByName("eyeMark").selected = true;
for (k;k < yclone;k++){
if (k === yclone) { break; }
myDuplicate = eyeMark.duplicate();
myDuplicate.position = [eyeMark.position[0] + horOffset * (0), eyeMark.position[1] + verOffset * (k + 1)];
}
}
########################################################################################## ###########################################