Skip to main content
evanc34113742
Participant
July 19, 2020
Answered

Apply random colour from swatch group to letters individually

  • July 19, 2020
  • 1 reply
  • 1429 views

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! 

This topic has been closed for replies.
Correct answer evanc34113742

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;
}

 

 


Yay!! That works perfectly!! Thank you so much. Hugely grateful to you. 

 

 

1 reply

femkeblanco
Legend
July 19, 2020

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;
}

 

 

evanc34113742
Participant
July 19, 2020

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

CarlosCanto
Community Expert
Community Expert
July 19, 2020

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;
}

 

 


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();