Highlighted

Apply random colour from swatch group to letters individually

Community Beginner ,
Jul 18, 2020

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! 

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

 

 

Topics

How to, Scripting, Third party plugins, Type

Views

145

Likes

Translate

Translate

Report

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

Apply random colour from swatch group to letters individually

Community Beginner ,
Jul 18, 2020

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! 

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

 

 

Topics

How to, Scripting, Third party plugins, Type

Views

146

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jul 18, 2020 0
Contributor ,
Jul 19, 2020

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

 

 

xxx.png

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 19, 2020 0
Community Beginner ,
Jul 19, 2020

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 19, 2020 0
Contributor ,
Jul 19, 2020

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.

 

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 19, 2020 0
Community Beginner ,
Jul 19, 2020

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 19, 2020 0
Contributor ,
Jul 19, 2020

Copy link to clipboard

Copied

Can you take a screen shot of your swatches panels?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 19, 2020 0
Community Beginner ,
Jul 19, 2020

Copy link to clipboard

Copied

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 19, 2020 0
Contributor ,
Jul 19, 2020

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

 

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 19, 2020 1
Community Beginner ,
Jul 19, 2020

Copy link to clipboard

Copied

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

 

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 19, 2020 0
Contributor ,
Jul 19, 2020

Copy link to clipboard

Copied

Glad I could help.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 19, 2020 1
Adobe Community Professional ,
Jul 19, 2020

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 19, 2020 1
Contributor ,
Jul 19, 2020

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

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 19, 2020 0