9 Replies Latest reply on Sep 23, 2016 10:24 PM by cy_auyeung

    Need script for find and change text

    John J David Level 1

      We have some set of texts that need to find and change in the entire InDesign CS4 document rather than doing manually. Kindly help me out to do the same successfully.

       

      for e.g. find text - "mam" and replace with "man".

        • 1. Re: Need script for find and change text
          Marijan Tompa [tomaxxi] Level 4

          Hey!

           

          This will do what you want:

           

          app.findTextPreferences = NothingEnum.nothing;
          app.changeTextPreferences = NothingEnum.nothing;
          
          app.findTextPreferences.findWhat = "mam";
          app.changeTextPreferences.changeTo = "man";
          
          //this will show changes count
          alert(app.activeDocument.changeText().length);
          
          app.activeDocument.changeText();
          

           

          --

          tomaxxi

          http://indisnip.wordpress.com/

          • 2. Re: Need script for find and change text
            Shonkyin Level 3

            Try below code if you have multiple texts for replace.

             

            replaceText ("mam", "man");
            replaceText ("mam1", "man1");
            
            
            function replaceText (input, output)
            {
                 app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;
                 app.findTextPreferences.findWhat = input;
                 app.changeTextPreferences.changeTo = output;
                 app.activeDocument.changeText();
                 app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing; 
            }
            

             

             

            Shonky

            • 3. Re: Need script for find and change text
              John J David Level 1

              Thanks for sending us the wonderful script. I need one more help on this.

               

              I need to find the character with the specific font and change to specific font

               

              or

               

              I need to find the character with the specific style and change to specific style

               

              Please help the same to do successfully.

              • 4. Re: Need script for find and change text
                Marijan Tompa [tomaxxi] Level 4

                Hey!

                 

                This is little extended version of script that Shonky wrote. You can search/find for text, paragraph styles, character styles, or in combination.

                 

                findChange(/*find text*/ 'mam', /*change to text*/ 'man', /*old style*/ '', /*new style*/ '', /*old char style*/ 'Bold Text', /*new char style*/ 'Extended Bold Text');
                
                function findChange (findWhat, changeTo, findStyle, changeStyle, findChrStyle, changeChrStyle){
                     app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;
                     if(findWhat)app.findTextPreferences.findWhat = findWhat;
                     if(changeTo)app.changeTextPreferences.changeTo = changeTo;
                     if(findStyle)app.findTextPreferences.appliedParagraphStyle = findStyle;
                     if(changeStyle)app.changeTextPreferences.appliedParagraphStyle = changeStyle;
                     if(findChrStyle)app.findTextPreferences.appliedCharacterStyle = findChrStyle;
                     if(changeChrStyle)app.changeTextPreferences.appliedCharacterStyle = changeChrStyle;
                     app.activeDocument.changeText();
                     app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;
                }
                

                 

                Hope it helps!

                 

                --

                tomaxxi

                http://indisnip.wordpress.com/

                • 5. Re: Need script for find and change text
                  ramkumar.vp Level 3

                  Hey,


                  Use the following code:


                  replaceTextByFont ("mam", "man");
                  replaceTextByStyle ("mam", "man");

                   


                  function replaceTextByStyle (input, output)
                  {
                      app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;
                      app.findTextPreferences.findWhat = input;
                      app.findTextPreferences.appliedParagraphStyle = app.activeDocument.paragraphStyles.item("SampleFindStyle");
                      app.changeTextPreferences.appliedParagraphStyle = app.activeDocument.paragraphStyles.item("SampleReplaceStyle");
                      app.changeTextPreferences.changeTo = output;
                      app.activeDocument.changeText();
                      app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;
                  }

                   

                  function replaceTextByFont (input, output)
                  {
                      app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;
                      app.findTextPreferences.findWhat = input;
                      app.findTextPreferences.appliedFont = app.fonts.item("Arial Bold");
                      app.changeTextPreferences.appliedFont = app.fonts.item("Arial BoldItalic");
                      app.changeTextPreferences.changeTo = output;
                      app.activeDocument.changeText();
                      app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;
                  }

                   

                   


                  Regards,

                  Ramkumar .P

                  • 6. Re: Need script for find and change text
                    Marijan Tompa [tomaxxi] Level 4

                    Hey Ramkumar!

                     

                    I think it's not good idea to change applied font because it will override applied paragraph/character style. Also, "Arial Bold" needs to be "Arial\tBold" and "Arial BoldItalic" needs to be "Arial\tBold Italic". You need "\t" because it separates font family name and font style.

                     

                    --

                    tomaxxi

                    http://indisnip.wordpress.com/

                    • 7. Re: Need script for find and change text
                      Oriup Level 1

                      Hi Marijan Tompa,

                      Can you help me?

                      How to find superscript?

                      My complete script is here:

                       

                      replace ("(.+)", "Italic", "", "SRF Cyan", "", "SRF_it");

                      replace ("(.+)", "Italic", "", "", "", "_it");

                      replace ("(.+)", "Bold", "", "SRF Cyan", "", "SRF_bld");

                      replace ("(.+)", "Bold", "", "", "", "_bld");

                      //replace ("(.+)", "", "position", "", "_^$1", "");

                      //replace ("", "", "", "", "", "");

                      function replace(findWhat, fontStyle, position, fillColor, changeTo, chrStyle){

                       

                          app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing;

                          if(findWhat)app.findGrepPreferences.findWhat = findWhat;

                          if(fontStyle)app.findGrepPreferences.fontStyle = fontStyle;

                      //    if(position)app.findGrepPreferences.postion = Position.SUPERSCRIPT;

                          if(fillColor)app.findGrepPreferences.fillColor = fillColor;

                          if(changeTo)app.changeGrepPreferences.changeTo = changeTo;

                          if(chrStyle)app.changeGrepPreferences.appliedCharacterStyle = app.activeDocument.characterStyleGroups.item("03_BodText").characterStyles.item(chrStyle) ;

                          app.activeDocument.changeGrep();

                          app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing;

                          };

                      • 8. Re: Need script for find and change text
                        Oriup Level 1

                        Hi Ramkumar,

                         

                        Please help me, how to find superscript? My complete script is here:

                         

                        replace ("(.+)", "Italic", "", "SRF Cyan", "", "SRF_it");

                        replace ("(.+)", "Italic", "", "", "", "_it");

                        replace ("(.+)", "Bold", "", "SRF Cyan", "", "SRF_bld");

                        replace ("(.+)", "Bold", "", "", "", "_bld");

                        //replace ("(.+)", "", "position", "", "_^$1", "");

                        //replace ("", "", "", "", "", "");

                        function replace(findWhat, fontStyle, position, fillColor, changeTo, chrStyle){

                         

                            app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing;

                            if(findWhat)app.findGrepPreferences.findWhat = findWhat;

                            if(fontStyle)app.findGrepPreferences.fontStyle = fontStyle;

                        //    if(position)app.findGrepPreferences.postion = Position.SUPERSCRIPT;

                            if(fillColor)app.findGrepPreferences.fillColor = fillColor;

                            if(changeTo)app.changeGrepPreferences.changeTo = changeTo;

                            if(chrStyle)app.changeGrepPreferences.appliedCharacterStyle = app.activeDocument.characterStyleGroups.item("03_BodText").characterStyles.item(chrStyle) ;

                            app.activeDocument.changeGrep();

                            app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing;

                            };

                         

                        Regards,

                        Sumit

                        • 9. Re: Need script for find and change text
                          cy_auyeung

                          Hi,

                           

                          It is very useful but it doesn't work in CS6? Help!

                           

                          Thanks

                           

                          cy