I am working on writing a script for Adobe Illustrator in javascript to fill objects with selected colours.
I have 32 objects and 12 colour swatches. I want to fill the 32 objects using each colour twice and then fill the remainder 8 objects with a random selection. I want to fill the objects in no discernible pattern, so just looping through each object and assigning it the next swatch colour will not do.
This is what I have so far, however it is not using each colour at least twice and filling at random.
myObjects = app.activeDocument.selection;
myDocument = app.activeDocument;
if (myObjects instanceof Array) {
colourSwatches = myDocument.swatches.getSelected();
//if there are swatches
if (colourSwatches.length != 0) {
for (i = 0; i < myObjects.length; i++) {
//if the selection is a path or compound path
if (myObjects[i].typename == "PathItem" || myObjects[i].typename == "CompoundPathItem") {
selItem = myObjects[i];
selItem.filled = true;
//select colour from swatches at random and then fill
swatchIndex = Math.round( Math.random() * (colourSwatches.length - 1 ));
if (selItem.typename == "PathItem") {
selItem.fillColor = colourSwatches[swatchIndex].color;
} else {
selItem.pathItems[0].fillColor = colourSwatches[swatchIndex].color;
}
}
}
}
}