I would like to write some scripts for randomising the allocation of swatches to adjacent object.
Example:
Imagine a map of Europe and all her nation states/territories. I have a User Defined Swatch Library. I can open the AI document it was made from as I understand from
that having the swatches as objects on the page makes them easier to be referenced in a script.
I want to iterate through the selected objects and randomly assign one of the colours in the swatch document as a fill to each object. It would be great if hidden swatches (they are all small rectangular 'colour chip' filled paths) on page were not included in the reference swatches randomly choosen from.
Iterating the selected objects I can do, but not sure what references I need to use to create an array of swatches to randomly choose from.
I see I can make an array of swatches using the general swatch group from Adobe's CreateSwatchGroup scripts:
JS
var docRef = app.documents.add(DocumentColorSpace.CMYK)
// Create a new SwatchGroup
var swatchGroup = docRef.swatchGroups.add();
swatchGroup.name = "CreateSwatchGroup";
// Get list of swatches in general swatch group
var genSwatchGroup = docRef.swatchGroups[0];
// Collect 5 random swatches from general swatch group and move to new group
var i = 0;
while (i < 5) {
var swatches = genSwatchGroup.getAllSwatches();
swatchCount = swatches.length;
var swatchIndex = Math.round(Math.random() * (swatchCount - 1)); // 0-based index
// New swatch group does not allow patterns or gradients
if (swatches[swatchIndex].color.typename != "PatternColor" && swatches[swatchIndex].color.typename != "GradientColor") {
swatchGroup.addSwatch(swatches[swatchIndex]);
i++;
}
}
// Updates swatch list with swatches moved to new swatch group
swatches = swatchGroup.getAllSwatches();
// [… etc etc]
AS
tellapplication "Adobe Illustrator"
activate
setdocReftomakenewdocumentwith properties {color space:CMYK}
-- Create a new SwatchGroup
setswatchGroupReftomakenewswatchgroupincurrent documentwith properties {name:"CreateSwatchGroup"}
-- Get list of swatches in general swatch group
setgenSwatchGrouptoswatchgroup 1 ofdocRef
-- Collect 5 random swatches from the general swatch group and move to new group
setito 0
repeatuntiliis 5
setswatchesReftoget all swatchesgenSwatchGroup
setswatchCounttocounteveryiteminswatchesRef
setswatchIndextorandom numberfrom 1 toswatchCount
setcurrentSwatchtoitemswatchIndexofswatchesRef
-- New swatch group does not allow patterns or gradients
ifclassofcolorofcurrentSwatchisnotpattern color infoandclassofcolorofcurrentSwatchisnotgradient color infothen
add swatchswatchGroupRefswatchcurrentSwatch
setitoi + 1
endif
endrepeat
-- [… etc etc]
If someone can help me to create the Swatch Array object of swatches loaded from a seperate document (or unique layer) then I think I can work my way to changing the fill on the existing paths (the nations in my map of Europe example).
Would be very cool if I could detect neighbooring paths (nieghbooring nations in my map of Europe example) make sure the colour being assigned is not within a certain hue/CMYK range of the random colour and if it is rechoose random colour. I have no idea how to perform the logic of determining neighbooring paths in an Illustartor script. Anybody?!
I'd prefer Applescript for sake of readiblity and also I'm learning AS ATM. Plus I'm using Script Debugger.app (which is excellent) to work with AS and I can't seem to run JSX scripts from within Extend Script Toolkit 2 (perhaps I need to make my scripts point to Illustrator?)
But Javascript is okay if someone has this covered already :-)