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

Help Changing Text Box Fill Color Based on Text

New Here ,
Aug 10, 2017 Aug 10, 2017

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. 

Screenshot 2017-08-10 11.06.40.png

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"

TOPICS
Scripting

Views

1.6K

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
community guidelines
New Here ,
Aug 10, 2017 Aug 10, 2017

Copy link to clipboard

Copied

I've found this post but it only works with cells and character styles.

Votes

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
community guidelines
Participant ,
Aug 10, 2017 Aug 10, 2017

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

}

Screen Shot 2017-08-11 at 5.10.47 PM.png

Votes

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
community guidelines
Participant ,
Aug 12, 2017 Aug 12, 2017

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

Votes

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
community guidelines
New Here ,
Aug 14, 2017 Aug 14, 2017

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? Screenshot 2017-08-14 15.53.54.png

Votes

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
community guidelines
Participant ,
Aug 15, 2017 Aug 15, 2017

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

Votes

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
community guidelines
New Here ,
Aug 15, 2017 Aug 15, 2017

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

Screenshot 2017-08-15 17.58.32.png

Votes

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
community guidelines
Participant ,
Aug 15, 2017 Aug 15, 2017

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

Votes

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
community guidelines
Participant ,
Aug 15, 2017 Aug 15, 2017

Copy link to clipboard

Copied

LATEST

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.

Votes

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
community guidelines