Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

Apply random colour from swatch group to letters individually

Community Beginner ,
Jul 18, 2020 Jul 18, 2020

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! 

TOPICS
Scripting , Third party plugins , Type
978
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Guide , Jul 19, 2020 Jul 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
...
Translate
Community Beginner , Jul 19, 2020 Jul 19, 2020

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

 

 

Translate
Adobe
Guide ,
Jul 19, 2020 Jul 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;
}

 

 

xxx.pngexpand image

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 19, 2020 Jul 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jul 19, 2020 Jul 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.

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 19, 2020 Jul 19, 2020

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!!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jul 19, 2020 Jul 19, 2020

Can you take a screen shot of your swatches panels?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 19, 2020 Jul 19, 2020
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jul 19, 2020 Jul 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;
}

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 19, 2020 Jul 19, 2020

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

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jul 19, 2020 Jul 19, 2020

Glad I could help.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 19, 2020 Jul 19, 2020

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();
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jul 19, 2020 Jul 19, 2020
LATEST

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

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines