5 Replies Latest reply on Jul 14, 2016 1:53 PM by Salah Fadlabi

    Text frame filled with background color based on word inside?

    AllaccessMitchell

      Hello Everyone,

       

      We do a lot of variable data merging here.

      When doing the variable data merge I'm looking to have the text frame fill with a specific color based on what word is within that box.

       

      Is there a way to accomplish this either during the merge or after the merge with a find and replace function or a script?

       

      For example if the text frame contains the word "meeting" then that frame's background color would be Red.

        • 1. Re: Text frame filled with background color based on word inside?
          Peter Spier Most Valuable Participant (Moderator)

          I think this would probably be pretty easy to script after the merge, and you should post over in InDesign Scripting

           

          I can think of a way to do this during the merge, too, but it's very cumbersome and would require modifying your data file.

           

          Essentially, you would need to add a new graphic frame to the template, and a field to the data set to hold links to a set of colored shapes that could be placed into the graphic frame. You could use an "IF" statement to populate the field with the correct file path to your colored shape based on the value in the text field.

          • 2. Re: Text frame filled with background color based on word inside?
            Skemicle Level 3

            Here is a script that will color the text box depending on the words contained. The words are case/punctuation sensetive so a search for "meeting" will not find "Meeting" or "meeting?" I have included the format of the word "meeting" though any additional word formats will need to be added. See http://indesignsecrets.com/how-to-install-a-script-in-indesign-that-you-found-in-a-forum-o r-blog-post.php

            for information on installing and using this script.

             

            //Scripted by Skemicle

            if (parseFloat(app.version) < 6)

            main();

            else

            app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Color Textboxes Based on Word");

            function main() {

                var sel = app.selection;

                for(s=0;s<sel.length;s++){

                    var cont = sel[s].parentStory.words

                    for(w=0;w<cont.length;w++){

                    //if statement will need to be copy/pasted for each additional word formatting

                        if(cont[w].contents == "meeting"){

                            sel[s].fillColor = "Red";

                        }

                    }

                }

            }

            • 3. Re: Text frame filled with background color based on word inside?
              Salah Fadlabi Level 5

              Hi

              • 4. Re: Text frame filled with background color based on word inside?
                Skemicle Level 3

                This script will add the color to all textboxes in the active document as long as the text frames are on a page. (text boxes located on the pasteboard will not be effected. The current formatting sets text boxes with "meeting" to red and "conference" to blue; just like with the first script you will need to add whatever additional formats you want by copy/pasting the if() statements.

                 

                 

                //Scripted by Skemicle

                if (parseFloat(app.version) < 6)

                main();

                else

                app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Color Textboxes Based on Word");

                function main() {

                    var myDoc = app.activeDocument;

                    var pages = myDoc.pages;

                    for(p=0;p<pages.length;p++){

                        var textFrames = pages[p].textFrames;

                        for(f=0;f<textFrames.length;f++){

                            var cont = textFrames[f].parentStory.words;

                            for(w=0;w<cont.length;w++){

                                if(cont[w].contents == "meeting"){

                                    textFrames[f].fillColor = "Red";

                                } if(cont[w].contents == "conference"){

                                    textFrames[f].fillColor = "Blue";

                                }

                            }

                        }   

                    }

                }

                1 person found this helpful
                • 5. Re: Text frame filled with background color based on word inside?
                  Salah Fadlabi Level 5

                  Thanks, that's very cool. Saved to my scripts collection.