4 Replies Latest reply: Jul 31, 2012 6:54 AM by caseyctg RSS

    Search for string in paragraph and apply a new character style?

    caseyctg Community Member

      I'm searching for a way to find a specific string in a specific text frame and make it bold. so my thought was to search and then apply a character style from my template doc. I thought this would work, but I went wrong somewhere.

       

      newDoc is my document. 

       

      //something like

      var Bios = "Representative Biography:\r want to leave this text normal.";

      var myTextFrameBios = newDoc.pages.item(i).textFrames.add({geometricBounds:[4.5277, 2.36, 8.625, 5.375], contents:Bios});

      var myTextObjectBios = myTextFrameBios.parentStory.texts.item(0);

       

      // IS THIS CONFLICTING with my change below? Can I apply a character style within a par styled frame?

      myTextObjectBios.appliedParagraphStyle = newDoc.paragraphStyles.item("CompanyProfile");

       

       

                                                        //Clear the find/change preferences.

                                                      app.findGrepPreferences = NothingEnum.nothing;

                                                      app.changeGrepPreferences = NothingEnum.nothing;

                                                      app.findTextPreferences.findWhat = "Representative Biography:";

                                                      app.changeTextPreferences.changeTo = "Representative Biography:";

       

       

                                                      app.findGrepPreferences.appliedCharacterStyle = newDoc.characterStyles.item("BoldBioHead");

                                                      // Perform the operation

                                                      app.documents.item(0).changeGrep();

       

                                                      app.findGrepPreferences = NothingEnum.nothing;

                                                      app.changeGrepPreferences = NothingEnum.nothing;

        • 1. Re: Search for string in paragraph and apply a new character style?
          caseyctg Community Member

          also tried this with no luck

           

          app.findGrepPreferences = null;

          app.changeGrepPreferences = null;

                                                          app.findGrepPreferences.findWhat =  "Representative Biography:";

                                                          //app.changeTextPreferences.changeTo = "Representative Biography:";

                                                          app.findGrepPreferences.appliedCharacterStyle = app.activeDocument.characterStyles.item("BoldBioHead");

          app.activeDocument.changeGrep();

          • 2. Re: Search for string in paragraph and apply a new character style?
            caseyctg Community Member

            this worked!

            app.findGrepPreferences = null;

            app.changeGrepPreferences = null;

            app.findTextPreferences.findWhat = "Representative Biography:";

            app.changeTextPreferences.appliedCharacterStyle = app.activeDocument.characterStyles.item("BoldBioHead");

            newDoc.changeText();

            app.changeTextPreferences = NothingEnum.nothing;

            app.findTextPreferences = NothingEnum.nothing;

            • 3. Re: Search for string in paragraph and apply a new character style?
              Laubender CommunityMVP

              @caseyctg – If you want to apply a character style to a text object in a specific text frame, you could restrict the scope of your script to that specific text frame:

               

              Example with the variable "myTextFrame" you have to define before:

               

              myTextFrame.changeText();
              

               

              Btw.: you are doing a text search and clearing your GREP find/change preferences at the start of the snippet? Why is that?

               

              You could do a:

               

              app.changeTextPreferences = app.findTextPreferences = NothingEnum.nothing;
              

               

              or:

               

              app.changeTextPreferences = app.findTextPreferences = null;
              

               

              set before the search and after the change, if you want to be on the safe side…

               

              Uwe

              • 4. Re: Search for string in paragraph and apply a new character style?
                caseyctg Community Member

                Thanks laubender. You were correct, I did need to update the GrepPreferences lines. Your suggestions worked great. Thanks.