Copy link to clipboard
Copied
Hi there,
I'm working on a range of typographic designs where each letter is individually coloured from a swatch group of 6 colours. What I'd like to do is have illustrator (or indesign) automatically work through the text applying one of those 6 colours, chosen at random to each letter, one by one. It would save me hours of laborious clicking and picking.
Does anyone have a script which might do this already? Or any pointers on how I might do this please?
Many thanks!
2 Correct answers
OK. I don't know that much about swatches, so someone might know an easier way. But since you're adding swatches to the end of the panel, I've targeted the collection backwards. So try this: (This will target the last six swatches.)
var min = app.activeDocument.swatches.length - 6;
var max = app.activeDocument.swatches.length - 1;
var letters = app.activeDocument.textFrames[0].textRange.characters;
for (var i = 0; i < letters.length; i++) {
var x = Math.floor(Math.random() * (max - min + 1
...
Yay!! That works perfectly!! Thank you so much. Hugely grateful to you.
Explore related tutorials & articles
Copy link to clipboard
Copied
Assuming one textFrame and six swatches, try this:
var letters = app.activeDocument.textFrames[0].textRange.characters;
for (var i = 0; i < letters.length; i++) {
var x = Math.floor(Math.random() * (7 - 1)) + 1;
letters[i].fillColor = app.activeDocument.swatches[x].color;
}
Copy link to clipboard
Copied
Oooh - thank you!! 🙂
Now, let's assume I'm incredibly thick!
Where would you suggest I paste that code? Do I need to make it into a jsx file?
Or is there somewhere in the Illustrator interface I can paste that?
Sorry for being a newbie on these scripts!
cheers,
Evan
Copy link to clipboard
Copied
Copy and paste it in a jsx file. (You can create a txt file and change the extension to jsx.)
Then, while your document is open in Illustrator, go to File > Scripts > Other Script (Ctrl+F12). Find your script and open it.
Copy link to clipboard
Copied
Thank you! I'm really grateful for this. Huge help!!
I had managed to get to the jsx stage and I've run the script. It works!!
However, it doesn't seem to be picking up my swatch colours. I've got a palette saved as "cute rainbow". That's open, and I've selected the colours within it. But when I run the script it isn't using those colours at all. Any idea what I should do?
Thanks again!!
Copy link to clipboard
Copied
Can you take a screen shot of your swatches panels?
Copy link to clipboard
Copied
This might be more helpful ...
https://www.loom.com/share/120bb4a6479e41328692e9dc1bf49a21
Copy link to clipboard
Copied
OK. I don't know that much about swatches, so someone might know an easier way. But since you're adding swatches to the end of the panel, I've targeted the collection backwards. So try this: (This will target the last six swatches.)
var min = app.activeDocument.swatches.length - 6;
var max = app.activeDocument.swatches.length - 1;
var letters = app.activeDocument.textFrames[0].textRange.characters;
for (var i = 0; i < letters.length; i++) {
var x = Math.floor(Math.random() * (max - min + 1)) + min;
letters[i].fillColor = app.activeDocument.swatches[x].color;
}
Copy link to clipboard
Copied
Yay!! That works perfectly!! Thank you so much. Hugely grateful to you.
Copy link to clipboard
Copied
Glad I could help.
Copy link to clipboard
Copied
you can target a Swatch Group instead of the whole Document Swatches
var idoc = app.activeDocument;
var colorgroup = idoc.swatchGroups['cute colors'];
var swatches = colorgroup.getAllSwatches();
Copy link to clipboard
Copied
Using what CarlosCanto suggested does make it neater. (Enter your colour group name in the first line below.)
var colorGroupName = "enter your color group name here";
var colorGroup = app.activeDocument.swatchGroups[colorGroupName].getAllSwatches();
var noOfColors = colorGroup.length;
var letters = app.activeDocument.textFrames[0].textRange.characters;
for (var i = 0; i < letters.length; i++) {
var x = Math.floor(Math.random() * noOfColors);
letters[i].fillColor = colorGroup[x].color;
}

