7 Replies Latest reply on Aug 21, 2013 4:38 PM by techzol

    Search and Replace Character Styles

    techzol

      Sorry if this has been asked and answered but I've not been able to find it looking in the forum.

       

      I'm doing technical documentation which we produce in both an interactive and print-only format. When it's done as an interactive PDF, the character styles are colored; when it's done for print, the color is stripped out.

       

      So, for example, I'd like to do a script which finds all text has the Character Style CrossReference and change it to Chartacter Style BoldUnderline.

       

      I'm needing a script that would let me cycle through about 6 different crossfererence styles and change them to their plain jane alternatives. I have do this for each chapter in the book and it covers 8 to 9 chapters per book. If there's a script that can do the entire book, great.

       

      Undoing it is no problem because I can just choose Update All Crossreferences from the book menu.

       

      Any help would be greatly appreciated.

        • 1. Re: Search and Replace Character Styles
          Learner Learner

          Hi techzol,

           

          Use this.

           

          var doc = app.activeDocument;

          var crfstyle = doc.characterStyles.item("CrossReference");

           

          app.findTextPreferences = NothingEnum.nothing;

          app.changeTextPreferences = NothingEnum.nothing;

          app.findTextPreferences.appliedCharacterStyle = crfstyle;

          app.changeTextPreferences.fontStyle = "Bold";

          app.changeTextPreferences.underline = true;

          doc.changeText()

          app.findTextPreferences = NothingEnum.nothing;

          app.changeTextPreferences = NothingEnum.nothing;

          • 2. Re: Search and Replace Character Styles
            Sajeev Sridharan Level 4

            Try this,

             

            myBookFile = File.openDialog("Choose the BOOK File", "INDB:*.indb", false);

            var myDoc=[];

            if(myBookFile != null){  

                app.open(File(myBookFile.fullName.toString()));

                var myBook = app.activeBook

                for(var n=0; n < myBook.bookContents.length; n++)

                {

                    myDoc[n] = app.open(File(myBook.bookContents[n].fullName.toString()));

                    //--- cross referece style 1

                    app.findGrepPreferences = app.changeGrepPreferences = null;

                    app.findGrepPreferences.appliedCharacterStyle = "crossreference1";

                    app.changeGrepPreferences.appliedCharacterStyle = "BoldUnderline";

                    app.activeDocument.changeGrep();

                    app.findGrepPreferences = app.changeGrepPreferences = null;

                    //--- cross referece style 2

                    app.findGrepPreferences = app.changeGrepPreferences = null;

                    app.findGrepPreferences.appliedCharacterStyle = "crossreference2";

                    app.changeGrepPreferences.appliedCharacterStyle = "BoldUnderline";

                    app.activeDocument.changeGrep();

                    app.findGrepPreferences = app.changeGrepPreferences = null;

                }

            }

            else{

                alert("Please select the book file properly !!!");

            }

             

             

            Vandy

            1 person found this helpful
            • 3. Re: Search and Replace Character Styles
              techzol Level 1

              Thanks for the suggestion. Haven't had a chance to try it yet, but it looks promising. Thanks for your help.

              • 4. Re: Search and Replace Character Styles
                techzol Level 1

                Same thanks to you as to learner. Will probably not get a chance to try until this weekend, but will let you know.

                • 5. Re: Search and Replace Character Styles
                  techzol Level 1

                  Vandy,

                   

                  Thanks, the script works and I can extend it as necessary for the other styles I need. Now, here's my delimma. Not all of the documents I need to sort through have all of the styles.

                   

                  I know I can inserta Try at the beginning and a Catch at the end. My searching hasn't shown me any code which basically tells it to ignore the error and keep going.

                   

                  Any thoughts?

                  • 6. Re: Search and Replace Character Styles
                    Asher31

                    Just wondering if you might be able to help me, i need to do the same thing as the OP but with paragraph styles, what would i need to change?

                    • 7. Re: Search and Replace Character Styles
                      techzol Level 1

                      You should be able to just change CharacterStyle to ParagraphStyle. i.e, instead of app.activeDocument.characterStyles.item, it would be app.activeDocument.paragraphStyles.item, etc. It should be that simple.

                       

                      Below is the entire script I'm currently using. I've refined it so that it checks to see if a document has changed and then saves it and then closes it. When the script is finished, I have an entire book that is ready to export to a print PDF.

                       

                      myBookFile = File.openDialog("Choose the BOOK File", "INDB:*.indb", false);

                      var myDoc=[];

                      if(myBookFile != null){ 

                          app.open(File(myBookFile.fullName.toString()));

                          var myBook = app.activeBook

                          for(var n=0; n < myBook.bookContents.length; n++)

                          {

                              myDoc[n] = app.open(File(myBook.bookContents[n].fullName.toString()));

                             var i, myFindStyle, myChangeStyle,
                             arr = [   ["CrossReference", "BoldUnderline"],
                                          ["CrossReferenceBoldItalic", "BoldItalicUnderline"],
                                          ["CrossReferenceBoldItalicCaps", "BoldItalicCaps"],
                                          ["CrossReferenceCaps", "BoldCaps"],
                                          ["CrossReferenceIndex", "Italic"],
                                    ];
                                     
                             for (i = 0; i < arr.length; i++) {       
                                   myFindStyle = app.activeDocument.characterStyles.item(arr[i][0]);
                                   myChangeStyle = app.activeDocument.characterStyles.item(arr[i][1]);
                         
                                  if (myFindStyle.isValid && myChangeStyle.isValid) {
                                      app.findGrepPreferences.appliedCharacterStyle = myFindStyle;
                                      app.changeGrepPreferences.appliedCharacterStyle = myChangeStyle;
                                      app.activeDocument.changeGrep();
                                  }

                             }
                            var i, myFindStyle, myChangeStyle,
                             arr = ["CrossReferenceProcedure", "CrossReferenceSection"];
                            myChangeStyle = app.activeDocument.characterStyles[0];         
                             for (i = 0; i < arr.length; i++) {       
                                   myFindStyle = app.activeDocument.characterStyles.item(arr[i]);
                         
                                  if (myFindStyle.isValid && myChangeStyle.isValid) {
                                      app.findGrepPreferences.appliedCharacterStyle = myFindStyle;
                                      app.changeGrepPreferences.appliedCharacterStyle = myChangeStyle;
                                      app.activeDocument.changeGrep();
                                  }
                             }

                           if (app.activeDocument.modified==true) {
                                app.activeDocument.save();
                                }
                            app.activeDocument.close();       

                          }

                      }
                      else{

                          alert("Please select the book file properly !!!");

                      }

                       

                      Hope this answers your question.