11 Replies Latest reply: Feb 24, 2012 9:45 PM by Tom Tomasko RSS

    [CS3 JS] How to search for accented letters with GREP

    Tom Tomasko Community Member

      Hello,

       

      In the following script I have two words, each that has an accented letter. It appears that neither in the script nor in the Find and Replace dialog that GREP will recognize words with accents. It will replace accented characters however. I can do a search for words with no accents but that will lead to trouble when I only want to change the word if it has an accent.

       

      Any advice would be great!

       

      Tom

       

      var myDoc = app.activeDocument;

      var rawWordsAccented = ["André","Barrës"]; //find these words

      var rawWordsAccentedDHyphens = ["~-Andr\\x{00E9}","~-Barr\\x{00EB}s"]; //replacement words

      for(var k =0; rawWordsAccented.length > k; k++){

          var numWords = theGrepChanger(myDoc,rawWordsAccented[k],rawWordsAccentedDHyphens[k]);   

          }//end for k

       

      function theGrepChanger(docRef,grepFindIt,grepChangeIt){

          app.findGrepPreferences = NothingEnum.NOTHING;

          app.changeGrepPreferences = NothingEnum.NOTHING;

          app.findGrepPreferences.findWhat = grepFindIt;

          app.changeGrepPreferences.changeTo = grepChangeIt;

          var arrGrepFindIt = myDoc.changeGrep();

          return arrGrepFindIt;

      }//end theGrepFinder

        • 1. Re: [CS3 JS] How to search for accented letters with GREP
          John Hawkinson CommunityMVP

          In the following script I have two words, each that has an accented letter. It appears that neither in the script nor in the Find and Replace dialog that GREP will recognize words with accents.

          Really? I can Find for "André" in the GREP tab in CS5. Works just fine.

          Did I msinterpret you?

          • 2. Re: [CS3 JS] How to search for accented letters with GREP
            pkahrel Community Member

            Tom,

             

            When you replace "Xyz" with "~-Xyz" you add a character to every found name, which messes up the references in the arrGrepFindIt array. Try looping backwards:

             

            for(var k =r awWordsAccented.length-1; k >= 0; k--)

             

            Peter

            • 3. Re: [CS3 JS] How to search for accented letters with GREP
              Tom Tomasko Community Member

              John, perhaps it works in CS5 but not in CS3.

               

              Peter, the problem is not in the replacement word or looping through that array backwards or forwards. The problem is finding a word with an accented character using the GREP mode.

               

              In using the Find/Replace dialog I cannot find André. Nor Andr\x{00E9}, using Unicode. So if the dialog won't work it appears a script won't work. If I use "Andre" in the Search field it will find "Andre" and "André."

               

              However I did find that if I change the GREP function in the script to the text mode I can find only words with accented characters and then replace them to my heart's content.

               

              Tom

               

               

               

              function theTextChanger(docRef,textFindIt,textChangeIt){
                  app.findTextPreferences = NothingEnum.NOTHING;
                  app.changeTextPreferences = NothingEnum.NOTHING;
                  app.findTextPreferences.findWhat = textFindIt;
                  app.changeTextPreferences.changeTo = textChangeIt;
                  var arrTextFindIt = myDoc.changeText();
                  return arrTextFindIt;
              }//end theTextChanger
              
              • 4. Re: [CS3 JS] How to search for accented letters with GREP
                pkahrel Community Member

                Tom,

                 

                You're right, looping forwards or backwards doesn't make any difference in this case.

                 

                Searching "André" works fine for me in CS3, though. Which language version are you using? I remember that in German and in French CS3 installations there were problems with several accented characters (mine is a US-English one, which, ironically, never had problems with accented characters but has difficulty with finding the dollar symbol $).

                 

                Peter

                • 5. Re: [CS3 JS] How to search for accented letters with GREP
                  Tom Tomasko Community Member

                  I assume I am using the English language version. How would I be able to find out?

                  • 6. Re: [CS3 JS] How to search for accented letters with GREP
                    pkahrel Community Member

                    If your menus and dialogs are in English, you have the English version. Strange that you can't find accents. I'm afraid I can't offer any help.

                    • 7. Re: [CS3 JS] How to search for accented letters with GREP
                      Laubender CommunityMVP

                      @Peter – you are right.

                       

                      Just tested with my German version of InDesign CS3 (v 5.0.4).

                       

                      GREP search could not find "André" nor "Andr\x{00E9}", text search will find "André".
                      Glyph search is also good if looking for "é".

                       

                      Uwe

                      • 8. Re: [CS3 JS] How to search for accented letters with GREP
                        Tom Tomasko Community Member

                        A related question is searching with case senstive and whole word properties in either Text or GREP mode.

                         

                        In the dialog GREP does not have these properites.

                         

                        In a script one can add for finding test:

                         

                        app.findChangeTextOptions.caseSensitive = true;

                        app.findChangeTextOptions.wholeWord = true;

                         

                        Is there a way in GREP in a script to do this?

                         

                        Tom

                        • 9. Re: [CS3 JS] How to search for accented letters with GREP
                          pkahrel Community Member

                          For case-insensitive searches, start your expression with (?i) (it's in the Modifier flyout in the Find/Change dialog). Thus, (?i)james matches James and james. Grep searches are not whole-word only by default. Use the word-boundary marker \b to search whole-word only: \brust\b matches rust but not trust.

                           

                          Peter

                          • 10. Re: [CS3 JS] How to search for accented letters with GREP
                            pkahrel Community Member

                            Uwe,

                             

                            You could use Andr[[=e=]] to find André, but it would find Andre too. [[=e=]] is a so-called character equivalent, meaning that it matches e and all accented versions of e.

                             

                            Peter

                            • 11. Re: [CS3 JS] How to search for accented letters with GREP
                              Tom Tomasko Community Member

                              Upon further research I have seen there is a difference between a PC and a Mac when using a GREP search.

                               

                              In the Search/Find dialog:

                               

                              Mac (version 10.5.8 and CS3): A GREP search for Andre will find both Andre and André. A search for André will find neither.

                               

                              PC (XP and CS3): A GREP search for Andre finds only Andre. A search for André finds only André.

                               

                              If I put this GREP search in the form of the script above, the script performs in like manner on a PC and Mac as the dialog search does.

                               

                              So why is there a difference between the two and that the PC is more gramatically correct than the Mac?

                               

                              And, more importantly, is there something in the script I can add to make it work on a Mac?

                               

                              I have since rewritten the script so that it does a Text search for words with accents rather than using a GREP search. This works fine on both platforms. But I still want to know what is going on here with GREP.

                               

                              Tom