2 Replies Latest reply on Aug 2, 2010 7:00 AM by [Jongware]

    [CS4 JS] Find and Change text to nothing

    stoereee Level 1



      I've have to following script that searches for word and changes to whole sentence to a paragraph style. I also want to remove the word I'm searching for. When I am using changeTo = ""; it will not change it because my value is nothing.



      var myDoc = app.activeDocument;
      function replaceTxt() {
         app.findTextPreferences = NothingEnum.nothing;
         app.changeTextPreferences = NothingEnum.nothing;
         app.findChangeTextOptions.wholeWord = false;
         app.findChangeTextOptions.caseSensitive = true;
         app.findChangeTextOptions.includeMasterPages = false;
            app.findTextPreferences.findWhat = "[Kop] ";
            app.changeTextPreferences.appliedParagraphStyle = myDoc.paragraphStyles.item("Kop"); 
            app.changeTextPreferences.changeTo = "";  // value is nothing so no changes???
         app.findTextPreferences = NothingEnum.nothing;
         app.changeTextPreferences = NothingEnum.nothing;



        • 1. Re: [CS4 JS] Find and Change text to nothing
          [Jongware] Most Valuable Participant

          Hoi Sjoerd,


          That's correct -- the same behavior is in the interface. If you find text and put nothing into change, the text is removed. However, if you find text but put just formatting into change, the formatting is applied and the text is not removed.


          To actually remove the text, you have a couple of options. First is to clear changeTo after your myDoc.changeText(); command, and run the changeText again -- that's how I'd do it in the interface itself.


          Somewhat more advanced would be using the result array that changeText(); returns, but (1) I couldn't get it to work , and (2) it's most likely fastest to run the changeText command twice.