Mobile app version of vmapp.org
Login or Join
Cooney243

: Convert smart (curly) quotes to dumb (straight) quotes in InDesign using JavaScript I am trying to write a script to automatically convert all curly double quotes (“”) in a text box into

@Cooney243

Posted in: #AdobeIndesign #IndesignScripting #Javascript

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:



imgtext.contents = imgtext.contents.replace(/[u201Cu201D]/g, '"');
imgtext.contents = imgtext.contents.replace(/[u201Cu201D]/g, "u0022");
imgtext.contents = imgtext.contents.replace(/[u201Cu201D]/g, '^"');
imgtext.contents = imgtext.contents.replace(/[u201Cu201D]/g, '^"');
imgtext.contents = imgtext.contents.replace(/[u201Cu201D]/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:



What am I missing here?

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Cooney243

1 Comments

Sorted by latest first Latest Oldest Best

 

@Turnbaugh909

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(/[u201Cu201D]/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.)

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme