8 Replies Latest reply on Aug 15, 2017 10:21 PM by Roy Marshall

    Help Changing Text Box Fill Color Based on Text

    Justin_Scalable Level 1

      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"

        • 1. Re: Help Changing Text Box Fill Color Based on Text
          Justin_Scalable Level 1

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

          • 2. Re: Help Changing Text Box Fill Color Based on Text
            Roy Marshall Level 1

            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[i];
                    myTextFrame.fillColor = myDoc.swatches.itemByName("PANTONE " +myTextFrame.paragraphs[0].parentStory.contents + " C" );
            }
            

             

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

            • 3. Re: Help Changing Text Box Fill Color Based on Text
              Roy Marshall Level 1

              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" );
              
              1 person found this helpful
              • 4. Re: Help Changing Text Box Fill Color Based on Text
                Justin_Scalable Level 1

                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

                • 5. Re: Help Changing Text Box Fill Color Based on Text
                  Roy Marshall Level 1

                  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

                  • 6. Re: Help Changing Text Box Fill Color Based on Text
                    Justin_Scalable Level 1

                    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[i];  
                            myTextFrame.fillColor = myDoc.swatches.itemByName("PANTONE " +myTextFrame.paragraphs[0].parentStory.contents + " C" );  
                    }  
                    

                     

                     

                    Screenshot 2017-08-15 17.58.32.png

                    • 7. Re: Help Changing Text Box Fill Color Based on Text
                      Roy Marshall Level 1

                      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[i]; 
                              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

                      • 8. Re: Help Changing Text Box Fill Color Based on Text
                        Roy Marshall Level 1

                        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[i]; 
                                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.