Font Replacement Scripting (JavaScript)
Copy link to clipboard
Copied
Hello,
I am trying to develop a Script for InDesign that would allow my team to find/replace multiple fonts throughout a document. We have standard fonts used throughout our English documents, but need to use different fonts when these files come back from translation. For example "Book Antiqua" needs to be changed to "Source Han Serif SC" so that Chinese characters can be displayed properly.
Every script that I have found to address this need has worked on MOST of the text, but there are several sections that do not change. Even in the Find/Replace (via Ctrl+F) dialog within InDesign has the exact result. However, when I use the Find/Replace menu via Type>Find Font, it works just fine on all text. This leads me to believe that the scripts I have been using emulate the behavior of the standard Ctrl+F Find and Replace action, but not the Type>Find Font action.
How can I emulate the action from Type>Find Font rather than Ctrl+F in a script? I am using ExtendScript for development. I would appreciate any help that could lead me in the right direction.
Thank you
Copy link to clipboard
Copied
Hello,
Try looking here for what you need: https://www.indesignjs.de/extendscriptAPI/indesign-latest/index.html#FindTextPreference.html
Regards,
Mike
Copy link to clipboard
Copied
Mike,
That is what I am currently using. It is only working on a small selection of text. Is there something that applies broadly to all text?
Copy link to clipboard
Copied
Some sample code to get you started.
app.findTextPreferences.appliedFont = "FontToFind";
app.changeTextPreferences.appliedFont = "FontToChangeTo";
app.activeDocument.changeText();
This mimics CTRL+F, applied to the whole document. If there are certain cases that are being missed, do you know why that is? Are they on locked layers, master pages, etc? If so, there are ways around that.
Copy link to clipboard
Copied
Hi,
can you show a code sample where a replace action will not work for you?
Together with a test document that is showing the issue.
Regards,
Uwe Laubender
( ACP )
Copy link to clipboard
Copied
Unfortunately, the documents contain proprietary information, but the code that I am using is as follows:
var mydoc = app.activeDocument;
var theFontReplacements = [
['Franklin Gothic Demi','Regular','Source Han Sans CN','Heavy'],
['Book Antiqua','Regular','Source Han Serif SC','Regular']
];
for (i = 0; i < theFontReplacements.length; i++) {
app.findTextPreferences = NothingEnum.nothing;
app.changeTextPreferences = NothingEnum.nothing;
app.findTextPreferences.appliedFont = theFontReplacements[i][0];
if (theFontReplacements[i][1] != ''){
app.findTextPreferences.fontStyle = theFontReplacements[i][1];
};
app.changeTextPreferences.appliedFont = theFontReplacements[i][2];
if (theFontReplacements[i][3] != ''){
app.changeTextPreferences.fontStyle = theFontReplacements[i][3];
};
mydoc.changeText();
};
Copy link to clipboard
Copied
We'll need an example of where it's not working. In theory, it should work broadly across the doc. Can you pinpoint an instance with screenshot of where it is not changed when it should, showing the font properties and the text frame configuration?
Copy link to clipboard
Copied
Hi Joseph,
as far as I can see your script is using only strings with the appliedFont property.
Perhaps you should change that to the "real" font object? Like:
/*
IMPORTANT: Note the syntax for the name
*/
var fontName = "Source Sans Pro"+"\t"+"Regular";
var fontCurrentlyApplied = app.fonts.itemByName( fontName );
if( fontCurrentlyApplied.isValid )
{
app.findTextPreferences.appliedFont = fontCurrentlyApplied ;
app.findGrepPreferences.appliedFont = fontCurrentlyApplied ;
}
else{ alert( "ERROR: Font not found." +"\r"+ fontName )};
And: You could test GREP instead of TEXT find-change.
Regards,
Uwe Laubender
( ACP )
Copy link to clipboard
Copied
Thank you for this suggestion. I will try to modify my code and do some testing with these changes in mind. I will let you know if it ends up fixing the issue. 🙂
Thank you,
Joseph
Copy link to clipboard
Copied
I went ahead and modified the code per your suggestions. It is still only replacing some of the text as before.
I'm sure that there is a way to get it working because all text is replaced when I use the tool via Type>Find Font. I just don't know how to script that same functionality.
Joseph
Copy link to clipboard
Copied
Again, it would be helpful if you share a screenshot of an instance where the font is not being replaced, or other way to identify which types of text are not being replaced. We can help you adjust the code if we know what it's missing. You can script a call to Find Font in the menu, but passing the arguments of which fonts to find and replace through scripting is nigh-impossible, if not outright impossible.
Copy link to clipboard
Copied
Hi,
I am actually able to share part of a document after all. I just included a single page for you to see. The issue is that when replacing the "Book Antiqua" font, it only replaces the font in the first couple paragraphs, but does not replace text in the later paragraphs.
Here is a link to the file, don't worry about missing images: https://www.dropbox.com/s/ewxljjjcx2nr8pc/fontChange.indd?dl=0
Apologies for my delay in response, things have been crazy these last couple weeks.
Joseph
Copy link to clipboard
Copied
There are two versions of Book Antiqua Regular in the document, so I think you’ll need to search for both.
"Book Antiqua (TT)"+"\t"+"Regular"
"Book Antiqua (OTF)"+"\t"+"Regular"
Copy link to clipboard
Copied
It appears that the text that I am currently able to replace is (OTF), but searching for (TT) is not returning anything.
Copy link to clipboard
Copied
Is the TT version showing as Missing in the Find Font dialog? If I try to search for a missing font via a script it returns nothing.
Copy link to clipboard
Copied
No it isn't. I am not even seeing a TT version at all.
Copy link to clipboard
Copied
It’s listed in Find Font for the doc you shared—I’m seeing (TT) Regular and (OTF) Regular. Everything is showing as missing for me because I don’t have the fonts.
Copy link to clipboard
Copied
This is what I am seeing in my Find Font window. It doesn't list OTF or TT after the font name.
Copy link to clipboard
Copied
Hi Joseph,
my Find Font window with InDesign 2020 is looking a bit different:
Exported the document to IDML, opened and saved it with InDesign CS6 and get quite a different Find Font window:
As you can see InDesign CS6 is listing Book Antiqua Regular only one time. InDesign 2020 two times…
I wonder what to do with that now.
Regards,
Uwe Laubender
( ACP )
Copy link to clipboard
Copied
Hi Uwe, What are the fonts at the top of the lists—there are 11 fonts and you are only showing 8 in your capture.
Copy link to clipboard
Copied
Hi Rob,
ah yes, from my InDesign 2020:
From my InDesign CS6:
Regards,
Uwe Laubender
( ACP )

