CSscripts icon indicating copy to clipboard operation
CSscripts copied to clipboard

Added support for groups for the Randomize Colors scripts

Open nickmainville opened this issue 9 years ago • 0 comments

var logStr=""; function log(pStr){ logStr+=pStr+""; }

function main(){

if ( app.documents.length == 0 ) {  
    return;     
}

doStuff();

return logStr;

}

function doStuff() {

var doc = app.activeDocument;
var canRect = doc.artboards[0].artboardRect;
var boardW = canRect[2];
var boardH = -canRect[3];

var swatches = doc.swatches.getSelected();

if(doc.selection.length==0) {
   alert("No items selected");        
} else if(swatches.length == 0) {
   alert("Please select swatches");
} else {
    findShapes
   randomizeColor(findShapes(doc.selection), swatches);
}

}

function findShapes (items) { var itemsList = []; for (var i = 0; i < items.length; i++) { if (items[i].typename === "GroupItem") { itemsList = itemsList.concat(findShapes(items[i].pageItems)); } else{ itemsList.push(items[i]); } }

    if(itemsList.length > 0){

    return itemsList;
        }

}

function randomizeColor (items, swatches) {

var count = items.length;

for (var i = 0; i < count ; i++) {                
    var c = items[i];
    var swatchIndex = Math.round( Math.random() * ( swatches.length - 1 ) );
    c.fillColor = swatches[ swatchIndex ].color;

   // var swatchIndex2 = Math.round( Math.random() * ( swatches.length - 1 ) );            
  // c.strokeColor = swatches[ swatchIndex2 ].color;
}

}

main();

nickmainville avatar Apr 24 '16 18:04 nickmainville