Skip to main content
evanc34113742
Participant
July 19, 2020
Resuelto

Apply random colour from swatch group to letters individually

  • July 19, 2020
  • 1 respuesta
  • 1429 visualizaciones

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! 

Este tema ha sido cerrado para respuestas.
Mejor respuesta de 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 respuesta

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

femkeblanco
Legend
July 19, 2020

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.