• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Font Replacement Scripting (JavaScript)

New Here ,
Apr 02, 2020 Apr 02, 2020

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

TOPICS
Scripting

Views

4.9K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Apr 02, 2020 Apr 02, 2020

Copy link to clipboard

Copied

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 02, 2020 Apr 02, 2020

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 02, 2020 Apr 02, 2020

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. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 02, 2020 Apr 02, 2020

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 )

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 07, 2020 Apr 07, 2020

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();
};

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 07, 2020 Apr 07, 2020

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? 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 07, 2020 Apr 07, 2020

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 )

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 09, 2020 Apr 09, 2020

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 14, 2020 Apr 14, 2020

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 15, 2020 Apr 15, 2020

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. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 21, 2020 Apr 21, 2020

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 21, 2020 Apr 21, 2020

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"

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 21, 2020 Apr 21, 2020

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 21, 2020 Apr 21, 2020

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 21, 2020 Apr 21, 2020

Copy link to clipboard

Copied

No it isn't. I am not even seeing a TT version at all.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 21, 2020 Apr 21, 2020

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.

 

Screen Shot.png

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 23, 2020 Apr 23, 2020

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.

 

Joseph_7878_0-1587662137631.png

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 23, 2020 Apr 23, 2020

Copy link to clipboard

Copied

Hi Joseph,

my Find Font window with InDesign 2020 is looking a bit different:

 

MissingFonts-1.PNG

 

Exported the document to IDML, opened and saved it with InDesign CS6 and get quite a different Find Font window:

 

MissingFonts-CS6.PNG

 

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 )

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 23, 2020 Apr 23, 2020

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 23, 2020 Apr 23, 2020

Copy link to clipboard

Copied

LATEST

Hi Rob,

ah yes, from my InDesign 2020:

 

MissingFonts-2.PNG

 

From my InDesign CS6:

 

MissingFonts-CS6-2.PNG

 

Regards,
Uwe Laubender

( ACP )

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines