8 Replies Latest reply on May 11, 2018 12:17 PM by pixxxel schubser

    Replace a letter using GREP without interfering in the GREP syntax

    manuelb27477138 Level 1

      Hello!

       

      Before the letter D, I try to insert a Column Break (in GREP is the character ^M) in my Index. But I can't because in GREP ^D means other thing and is not working the script.

      Is important be sure don't damage other letters D

      How can I add a column break before my single letter D ?

       

      replaceText ("^D\r","^MD\r") 
      
      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;   
      } 
      

       

      My actual Index:

      Screen Shot 2018-05-11 at 16.22.59.png

       

      The result I expect:

      Screen Shot 2018-05-11 at 16.23.27.png

       

       

      Thanks so much in advance!

        • 1. Re: Replace a letter using GREP without interfering in the GREP syntax
          pixxxel schubser MVP & Adobe Community Professional

          Try this:

          SpaltenumbruchVorD.png

           

          Have fun

           

           

          Aktualisiert

          (too much security)

          ^(D)$  is sufficient

          • 2. Re: Replace a letter using GREP without interfering in the GREP syntax
            manuelb27477138 Level 1

            Hi !

             

            Is funny because in Indesign using the normal Panel Find/change is working.

            But with my code is not working. Do you thing this kind of GREP are not working in the scripts? or maybe is something wrong in my script?

             

            replaceText ("^([D])$","~M$1")
            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; 
            }
            

             

             

            Thanks a lot !

            • 3. Re: Replace a letter using GREP without interfering in the GREP syntax
              pixxxel schubser MVP & Adobe Community Professional

              You cannot use changeGrep with TextPreferences syntax.

              This should work.

              replaceText ("^(D)$","~M$1")
              function replaceText (input, output) 
              { 
                  app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing; 
                  app.findGrepPreferences.findWhat = input;
                  app.changeGrepPreferences.changeTo = output;
              
                  app.activeDocument.changeGrep(); 
                  app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing; 
              }
              

               

              Have fun

               

              • 5. Re: Replace a letter using GREP without interfering in the GREP syntax
                manuelb27477138 Level 1

                here my final script, maybe help to someone:

                 

                // Source: https://forums.adobe.com/thread/708641
                // This Script allow you to find a replace a Text using 2 syntax FIND or GREP. Becarefull, affects the text of the entire document. The sintaxis of your queries are different if you use FIND or GREP, exaclty same like when you find and replace with the indesign window.
                // https://helpx.adobe.com/indesign/using/find-change.html#find_and_change_objects
                // To replace with GREP a simple Index Setion Heading, for example the letter D, you can use simple this ^(D)$ but if you want extra security use this ^([D])$
                
                
                /******** FIND AND REPLACE TEXT USING GREP ********/
                replaceTextUsingGREP ("^([D])$","~M$1"); // add a Break Column before letter D (Index Section Heading) 
                function replaceTextUsingGREP (input, output) 
                { 
                    app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing; 
                    app.findGrepPreferences.findWhat = input;
                    app.changeGrepPreferences.changeTo = output;
                    app.activeDocument.changeGrep(); 
                    app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing; 
                }
                
                
                
                
                /******** FIND AND REPLACE TEXT USING FIND ********/
                replaceTextUsingFIND ("yourText", "^MyourText");  //add a Break Column
                replaceTextUsingFIND ("yourText2\r", "yourText2\r");  // add many queries as you want
                function replaceTextUsingFIND (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;   
                }
                
                • 6. Re: Replace a letter using GREP without interfering in the GREP syntax
                  pixxxel schubser MVP & Adobe Community Professional

                  You're welcome.

                   

                  (But please change 'extra security' to 'unnecessary'.)

                  • 7. Re: Replace a letter using GREP without interfering in the GREP syntax
                    manuelb27477138 Level 1

                    Here the final code, thanks to pixxxel schubser!

                     

                     

                    // Source: https://forums.adobe.com/thread/708641  
                    // This Script allow you to find a replace a Text using 2 syntax FIND or GREP. Becareful, affects the text of the entire document. The sintaxis of your queries are different if you use FIND or GREP, exactly same like when you find and replace with the indesign window. 
                    // https://helpx.adobe.com/indesign/using/find-change.html#find_and_change_objects  
                    // To replace with GREP a simple Index Section Heading, for example the letter D, you can use simple this ^(D)$ but if you want unnecessary use this ^([D])$ 
                    
                    
                    /******** FIND AND REPLACE TEXT USING GREP ********/ 
                    replaceTextUsingGREP ("^(D)$","~M$1"); // add a Break Column before letter D (Index Section Heading)  
                    function replaceTextUsingGREP (input, output)  
                    {  
                        app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing;  
                        app.findGrepPreferences.findWhat = input; 
                        app.changeGrepPreferences.changeTo = output; 
                        app.activeDocument.changeGrep();  
                        app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing;  
                    } 
                    
                    
                    
                    
                    /******** FIND AND REPLACE TEXT USING FIND ********/ 
                    replaceTextUsingFIND ("yourText", "^MyourText");  //add a Break Column 
                    replaceTextUsingFIND ("yourText2\r", "yourText2\r");  // add many queries as you want 
                    function replaceTextUsingFIND (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;    
                    }
                    
                    • 8. Re: Replace a letter using GREP without interfering in the GREP syntax
                      pixxxel schubser MVP & Adobe Community Professional

                      manuelb27477138  schrieb

                       

                      … you can use simple this ^(D)$ but if you want unnecessary use this ^([D])$  …

                       

                      That's really funny. I'm amused. Thank you