Skip to main content
Inspiring
January 5, 2023
Question

Javascript find/change fails

  • January 5, 2023
  • 3 replies
  • 4403 views
Hi all
 
I've written a small javascript to replace tags in a document with formatting, but it's not working, and I have no idea why. I know that the code below is correctly populating the find and change fields in the dialog, and I know that if I open the find/change dialog and click "Find Next," this works. Run from the Javascript, however, the actual find/change is not effected. Can anyone see a problem here that I'm missing? (This is the complete code other than a couple comments.)
 
Thanks!
 
m.
 
-----
 
app.findChangeGrepOptions.includeFootnotes = true;
app.findGrepPreferences.findWhat = "¦case:all_caps¦(.+)¦/case¦";
app.changeGrepPreferences.changeTo="$1";
app.changeGrepPreferences.capitalization=Capitalization.ALL_CAPS;
app.activeDocument.changeGrep(); //This line seems not to work
This topic has been closed for replies.

3 replies

Community Expert
January 7, 2023

Hi @Matthew Williams CMH,

You code worked for me on my test document. The only thing I would change in your code is to edit the grep expression to

"¦case:all_caps¦(.+?)¦/case¦"

Can you share a document where it does not work you?

-Manan

-Manan
m1b
Community Expert
Community Expert
January 7, 2023

Hi @Manan Joshi, search on this thread for words "XML Testing" and you will find that @Matthew Williams CMH has shared a test .indd. Also read my posts if you haven't. It's a strange problem—out of my area of knowledge. I'm interested to see what you make of it.

- Mark

Robert at ID-Tasker
Legend
January 7, 2023

I still got no chance to check mentioned INDD file - but if there are XML tags inside the text - it should be possible to add them to the grep to ignore / treat as part of the text? 

 

0xFEFF

 

m1b
Community Expert
Community Expert
January 5, 2023

Hi @Matthew Williams CMH, I'm not at my computer to test right now but I think you can solve this problem by saving your script file as "UTF-8 with BOM". You can do this in some feature-rich text editors. I use VSCode. With the script open in VSCode click the encoding near the bottom right of window and re-save as "UTF-8 with BOM". See if that makes a difference.

an alternative is to manually encode the special characters eg the split bar as \u0000 where 0000 is the hex Unicode for that character. 

- Mark

Inspiring
January 5, 2023

Thanks for weighing in, Mark. I tried both suggestions, and still no dice.

 

Here is a new wrinkle that may suggest a way to correct this. I tried shortening my find string to see if perhaps I was overlooking an error in the grep, and something really weird happend. If I search for "¦case:a", the script works. If I search for "¦case:al", though, it fails. Why just adding that "el" is causing the script not to work I can't imagine.

m1b
Community Expert
Community Expert
January 5, 2023

Okay I just tried your script, as originally posted and it seemed to work as expected.

 

Maybe you have another find or change grep preference set already. Can you add these two lines to the start of your test script and try it out?

app.findGrepPreferences = NothingEnum.NOTHING;
app.changeGrepPreferences = NothingEnum.NOTHING;

 - Mark

Robert at ID-Tasker
Legend
January 5, 2023

I think you need to "escape" "/' before the "case":

 

"\/case"

 

Or rather all "¦" - or are they "|"?

 

Inspiring
January 5, 2023

Hi Robert

Thanks for your thoughts. Unfortunately, no go. (They're split pipes, not solid pipes.) What's weird is that the find works if I let the script populate the find field but manually execute it with the Find Next button in the dialog. It's like the last line of the script is being ignored.

 

Robert at ID-Tasker
Legend
January 5, 2023

Sorry, stupid answer 😞😞‌ been looking on someone else's code 😞

 

Can you try to execute findGrep() and see if you have any results?