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
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
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
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
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
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
North America
Europe, Middle East and Africa
Asia Pacific