0

I am trying to write a script to automatically convert all curly double quotes (“”) in a text box into straight double quotes ("). Here's all that I've tried so far and none of them seem to be working:

  1. imgtext.contents = imgtext.contents.replace(/[\u201C\u201D]/g, '"');

  2. imgtext.contents = imgtext.contents.replace(/[\u201C\u201D]/g, "\u0022");

  3. imgtext.contents = imgtext.contents.replace(/[\u201C\u201D]/g, '\^"');

  4. imgtext.contents = imgtext.contents.replace(/[\u201C\u201D]/g, '^\"');

  5. imgtext.contents = imgtext.contents.replace(/[\u201C\u201D]/g, '\^\"');

The Find/Change option in the Edit menu works perfectly fine and is exactly the functionality I'm trying to replicate using my script. Here's what my Find/Change dialog looks like:

enter image description here

What am I missing here?

1

The problem is that in your InDesign preferences you have enabled "Use Typographer's Quotes". Every time you write plain text into a text frame, ID checks to see if it's a plain single ' or double "; and if so, it immediately changes it to the proper curly equivalent. You can see that if you run your script on a text box that contains both single and double quotes; not only will it leave your existing double quotes unchanged, but in addition all straight double and single quotes will magically curl!

You can fix this for your current script by changing the global setting of this preference to false, changing the text, and then resetting it to its original value (as in "Be Kind To Your Users"):

oldPref = app.activeDocument.textPreferences.typographersQuotes;
app.activeDocument.textPreferences.typographersQuotes = false;
imgtext = app.selection[0];
imgtext.contents = imgtext.contents.replace(/[\u201C\u201D]/g, '"');
app.activeDocument.textPreferences.typographersQuotes = oldPref;

However, when testing I found lots more little things changed as well. This is because changing the textual content of a frame this way deletes the entire text, then inserts it again. That means that any and all 'local' formatting will disappear, and all text in the box will get the formatting of the very first character.

A better way is to not interact with the plain text through Javascript, but to change the text in your frame with InDesign's own changeText – which does exactly the same thing as in the interface when using the Find/Change dialog.

In Javascript, this would be:

imgtext = app.selection[0];
app.findTextPreferences = app.changeTextPreferences = null;
app.findTextPreferences.findWhat = '"';
app.changeTextPreferences.changeTo = '^"';
imgtext.changeText();

but much to my surprise, I 'see' the changes being made one by one! So this may be some kind of 'hack' by Adobe's programmers. If you disable Typographic Quotes and just replace " by ", you won't be seeing those weird redraw artifacts:

oldPref = app.activeDocument.textPreferences.typographersQuotes;
app.activeDocument.textPreferences.typographersQuotes = false;
imgtext = app.selection[0];
app.findTextPreferences = app.changeTextPreferences = null;
app.findTextPreferences.findWhat = '"';
app.changeTextPreferences.changeTo = '"';
imgtext.changeText();
app.activeDocument.textPreferences.typographersQuotes = oldPref;

Note that other than Javascript's replace, you do not have to specify anything special to catch all kinds of curly quotes. When given a text with a " anywhere, InDesign will automatically find both that and all its curly variants.

(In case you need it some time: to find only straight ", you would indeed prefix it with a ^ in the Find dialog. To find only curly “” you need to copy them into the Find dialog or use the ^{ and ^} shortcuts.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.