Copy link to clipboard
Copied
I have several text boxes I would like to change the fill color of based on whatever text is in that text box. As pictured below, if a text box contains "268" the set the box color to Pantone "268" and so on and so forth.
How do I write a script for all 4 text boxes (different Pantones # in each one) to change the color to the corresponding Pantone Color (all swatches exist in my library)?
Any help would be much appreciated.
Primary Pantone Box
If <<Text1>> contains "268"
then Set Box Color to: "PANTONE 268 C"
else if <<Text1>> contains "421"
then Set Box Color to: "PANTONE 421 C"
else if <<Text1>> contains "429"
then Set Box Color to: "PANTONE 429 C"
else if <<Text1>> contains "black"
then Set Box Color to: "Black"
else if <<Text1>> contains "white"
Copy link to clipboard
Copied
I've found this post but it only works with cells and character styles.
Copy link to clipboard
Copied
If you are not restricting yourselves to the above colours, then this will colour each box with the number contained in the text frame:
var myDoc = app.activeDocument;
for(var i= 0; i<=myDoc.textFrames.length-1; i++){
var myTextFrame = myDoc.textFrames;
myTextFrame.fillColor = myDoc.swatches.itemByName("PANTONE " +myTextFrame.paragraphs[0].parentStory.contents + " C" );
}
Copy link to clipboard
Copied
or as you mentioned, so see if the contents "contains" the text, grep it...
var myText = myTextFrame.paragraphs[0].parentStory.contents.match(/\d{3,4}/);
myTextFrame.fillColor = myDoc.swatches.itemByName("PANTONE " +myText + " C" );
Copy link to clipboard
Copied
Thanks for the replies! I'm new to scripting with javascript & Indesign so I may not be doing this right as evident by this error I'm receiving. Any ideas?
Copy link to clipboard
Copied
Hey.
That error implies that the swatch the script is looking for doesn't exist.
Add this line between line 2 and 3:
alert("Swatch Color name is: PANTONE " + myTextFrame.paragraphs[0].parentStory.contents + " C" );
This put an alert on your screen letting you know what swatch it is trying to use, and you can confirm then that your swatch palette contains this swatch.
Cheers
Roy
Copy link to clipboard
Copied
Hi Roy,
Apologies, as I feel my ignorance is showing here. I'm using the following code but now I get a new error. I wouldn't doubt it if this is a syntax error as I'm constantly breaking code because of my formatting.
var myDoc = app.activeDocument;
for(var i= 0; i<=myDoc.textFrames.length-1; i++){
alert("Swatch Color name is: PANTONE " + myTextFrame.paragraphs[0].parentStory.contents + " C" );
var myTextFrame = myDoc.textFrames;
myTextFrame.fillColor = myDoc.swatches.itemByName("PANTONE " +myTextFrame.paragraphs[0].parentStory.contents + " C" );
}
Copy link to clipboard
Copied
My apologies. Switch the order of the lines 3 and 4.
var myDoc = app.activeDocument;
for(var i= 0; i<=myDoc.textFrames.length-1; i++){
var myTextFrame = myDoc.textFrames;
alert("Swatch Color name is: PANTONE " + myTextFrame.paragraphs[0].parentStory.contents + " C" );
myTextFrame.fillColor = myDoc.swatches.itemByName("PANTONE " +myTextFrame.paragraphs[0].parentStory.contents + " C" );
}
Sorry. Should have ran a quick check, but I was not at my computer!
Cheers
Copy link to clipboard
Copied
Personally I would rather have a bit more error checking and feedback as there are many things that can cause a fail.
var myDoc = app.activeDocument;
for(var i= 0; i<=myDoc.textFrames.length-1; i++){
var myTextFrame = myDoc.textFrames;
try{
alert("Swatch Color name is: PANTONE " + myTextFrame.paragraphs[0].parentStory.contents + " C" );
myTextFrame.fillColor = myDoc.swatches.itemByName("PANTONE " +myTextFrame.paragraphs[0].parentStory.contents + " C" );
}
catch(e){
if(myTextFrame.paragraphs[0].isValid == false){
alert("ERROR\n" + e + "\nCannot find any text in the Text Box");
}else{
alert("ERROR\n" + e + "\nCannot find colour called PANTONE "+myTextFrame.paragraphs[0].parentStory.contents + " C");
}
}
}
This will look for the Pantone Number in the Text Frame, and try to colour it using the text in the text frame. If if cant, it will tell you the swatch that it is looking for, and continue to the next.
If the Text frame is empty it will tell you that too.