Skip to main content
Participating Frequently
April 2, 2020
Question

Font Replacement Scripting (JavaScript)

  • April 2, 2020
  • 5 replies
  • 10321 views

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

This topic has been closed for replies.

5 replies

Community Expert
April 23, 2020

Hi Rob,

ah yes, from my InDesign 2020:

 

 

From my InDesign CS6:

 

 

Regards,
Uwe Laubender

( ACP )

Community Expert
April 23, 2020

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 )

rob day
Community Expert
Community Expert
April 23, 2020

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.

Community Expert
April 7, 2020

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 )

Participating Frequently
April 9, 2020

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

Participating Frequently
April 14, 2020

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

Community Expert
April 2, 2020

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 )

Participating Frequently
April 7, 2020

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();
};
brian_p_dts
Community Expert
Community Expert
April 7, 2020

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? 

Legend
April 2, 2020
Participating Frequently
April 2, 2020

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?

brian_p_dts
Community Expert
Community Expert
April 2, 2020

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.