2 Replies Latest reply on Apr 26, 2009 2:22 PM by jw24

    Scripting for search/replace

    jw24

      I created 6 Search/Replace items to help with changing the format of a document.  I would like to get a script that will run them in order.  Can anyone help me to do this or point me in the right direction? I'd really appreciate it. Thanks!!

        • 1. Re: Scripting for search/replace
          Kasyan Servetsky Level 5

          Here is my way of performing a series of find/change operations:

          First I create an array, each element of which contains a find/change pair. Then I process it in a function:

          // for text

          var myTxtReplace = [

                             ["«", "“"],

                             ["»", "”"],

                             ]

          function replaceTxt() {
              app.findTextPreferences = NothingEnum.nothing;
              app.changeTextPreferences = NothingEnum.nothing;
              app.findChangeTextOptions.wholeWord = false;
              app.findChangeTextOptions.caseSensitive = true;
              app.findChangeTextOptions.includeMasterPages = false;
              for (i = 0; i <  myTxtReplace.length; i++) {
                  var myCurArray = myTxtReplace[i];
                  app.findTextPreferences.findWhat = myCurArray[0];
                  app.changeTextPreferences.changeTo = myCurArray[1];
                  myTextFrame.parentStory.changeText();
              }
              app.findTextPreferences = NothingEnum.nothing;
              app.changeTextPreferences = NothingEnum.nothing;
          }

           

          // for GREP

          myGrpReplace =[
                         ["[~m~>~f~|~S~s~<~/~.~3~4~% ]{2,}", " "], // Multiple Space to Single Space
                         ["--", "~_"], // Dash Dash to Em-dash
                         ["~b~b+", "\\r"] // Multiple Return to Single Return
                         ]

          function replaceGrep() {
              app.findGrepPreferences = NothingEnum.nothing;
              app.changeGrepPreferences = NothingEnum.nothing;
              for (i = 0; i <  myGrpReplace.length; i++) {
                  var myCurArray = myGrpReplace[i];
                  app.findGrepPreferences.findWhat = myCurArray[0];
                  app.changeGrepPreferences.changeTo = myCurArray[1];
                  myTextFrame.parentStory.changeGrep();
              }
              app.findGrepPreferences = NothingEnum.nothing;
              app.changeGrepPreferences = NothingEnum.nothing;
          }

           

          Another option is to use FindChangeByList.jsx script – it’s pre-installed with incopy.

                             

          • 2. Re: Scripting for search/replace
            jw24 Level 1

            Thanks for the answer - I think that the FindChangeList is going to work out perfectly for me.  You saved me tons of time!