Skip to main content
Known Participant
July 16, 2019
Answered

Changing Fonts via script

  • July 16, 2019
  • 3 replies
  • 8122 views

my group of designers has been told that we are to no longer use the type 1 version of the font Whitney and to change all documents going forward to the open type font version. (they actually removed the (T1) font from the computer!)

so now all my old documents open with lots of type highlighted warning me that the font is missing. I can manually go thru and change every document when i open it using the built in "find font" multiple times because of the various weights. or with your help i can figure out a script that will do it for me.

It seems straight forward but when i go looking for an existing script to start from they all seem to run into some sort of roadblock...i haven't found a single one that claims to actually work (they all give up before accomplishing writing this script)

ideally this script would look for Whitney (T1) Bold and replace it with Whitney Bold

Whitney (T1) book and replace it with ....

and also do the same this in the style/character sheets

anybody up for the challenge?

scott

This topic has been closed for replies.
Correct answer daitranthanhoa

you can use this script to replace font of all Text, Character Styles, Paragraph Styles:

//Old Font

var oldFontName="Whitney (T1)"

var oldFontStyle="Bold"

//New Font

var newFontName= "Whitney";

var newFontStyle="Bold";

var myDoc = app.activeDocument;

//Replace font of Text

app.findTextPreferences=app.changeTextPreferences= NothingEnum.nothing; 

app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing; 

app.findTextPreferences.appliedFont = oldFontName;

app.findTextPreferences.fontStyle = oldFontStyle;

app.changeTextPreferences.appliedFont =newFontName;

app.changeTextPreferences.fontStyle =newFontStyle;

myDoc.changeText(); 

var oStyle;

//Replace font of character styles

for(var i=0;i<myDoc.characterStyles.length;i++)

{

    oStyle=myDoc.characterStyles;

    try

    {

        var currentFont=oStyle.appliedFont ;

        if (oStyle.fontStyle != "" )

               currentFont = currentFont  + oStyle.fontStyle;

        if(currentFont==oldFontName+oldFontStyle)

        {

              oStyle.appliedFont =newFontName;

            if(newFontStyle !="")

              oStyle.fontStyle=newFontStyle;

        }

        

    }

    catch(e)

    {}

  

}

//Replace font of paragraph styles

for(var i=0;i<myDoc.paragraphStyles.length;i++)

{

    oStyle=myDoc.paragraphStyles;

    try

    {

        var currentFont=oStyle.appliedFont.fontFamily;

        if (oStyle.fontStyle != "" )

               currentFont = currentFont  + oStyle.fontStyle;

        if(currentFont==oldFontName+oldFontStyle)

        {

              oStyle.appliedFont =newFontName;

            if(newFontStyle !="")

              oStyle.fontStyle=newFontStyle;

        }

        

    }

    catch(e)

    {}

  

}

3 replies

daitranthanhoa
daitranthanhoaCorrect answer
Inspiring
July 18, 2019

you can use this script to replace font of all Text, Character Styles, Paragraph Styles:

//Old Font

var oldFontName="Whitney (T1)"

var oldFontStyle="Bold"

//New Font

var newFontName= "Whitney";

var newFontStyle="Bold";

var myDoc = app.activeDocument;

//Replace font of Text

app.findTextPreferences=app.changeTextPreferences= NothingEnum.nothing; 

app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing; 

app.findTextPreferences.appliedFont = oldFontName;

app.findTextPreferences.fontStyle = oldFontStyle;

app.changeTextPreferences.appliedFont =newFontName;

app.changeTextPreferences.fontStyle =newFontStyle;

myDoc.changeText(); 

var oStyle;

//Replace font of character styles

for(var i=0;i<myDoc.characterStyles.length;i++)

{

    oStyle=myDoc.characterStyles;

    try

    {

        var currentFont=oStyle.appliedFont ;

        if (oStyle.fontStyle != "" )

               currentFont = currentFont  + oStyle.fontStyle;

        if(currentFont==oldFontName+oldFontStyle)

        {

              oStyle.appliedFont =newFontName;

            if(newFontStyle !="")

              oStyle.fontStyle=newFontStyle;

        }

        

    }

    catch(e)

    {}

  

}

//Replace font of paragraph styles

for(var i=0;i<myDoc.paragraphStyles.length;i++)

{

    oStyle=myDoc.paragraphStyles;

    try

    {

        var currentFont=oStyle.appliedFont.fontFamily;

        if (oStyle.fontStyle != "" )

               currentFont = currentFont  + oStyle.fontStyle;

        if(currentFont==oldFontName+oldFontStyle)

        {

              oStyle.appliedFont =newFontName;

            if(newFontStyle !="")

              oStyle.fontStyle=newFontStyle;

        }

        

    }

    catch(e)

    {}

  

}

@Js1212Author
Known Participant
July 22, 2019

i pulled from the above and a few other scripts and have something that is mostly working...

what i am not able to address is if the document has a bulleted list. The bullets typeface are defined inside of the Paragraph style under bullets and numbering. this does not change when i change the font for character and/or paragraph fonts.

how do i change the font of "bullets and numbers"?

scott

here is what i have so far....

var fontList = [ 

    ["Janson Text\t55 Roman", "Whitney (OTF)\tBook"], 

    ["Whitney (T1)\tBook", "Whitney (OTF)\tBook"], 

   ["Whitney (T1)\tRegular", "Whitney (OTF)\tRegular"], 

    ["Whitney (T1)\tMedium", "Whitney (OTF)\tMedium"], 

    ["Whitney (T1)\tSemibold", "Whitney (OTF)\tSemibold"], 

    ["Whitney (T1)\tBold", "Whitney (OTF)\tBold"], 

    ["Whitney (T1)\tBlack", "Whitney (OTF)\tBlack"], 

    //

    ["Whitney (T1)\tBookItalic", "Whitney (OTF)\tBookItalic"], 

    ["Whitney (T1)\tRegularItalic", "Whitney (OTF)\tRegularItalic"], 

    ["Whitney (T1)\tMediumItalic", "Whitney (OTF)\tMediumItalic"], 

    ["Whitney (T1)\tSemiboldItalic", "Whitney (OTF)\tSemiboldItalic"], 

    ["Whitney (T1)\tBoldItalic", "Whitney (OTF)\tBoldItalic"], 

    ["Whitney (T1)\tBlackItalic", "Whitney (OTF)\tBlackItalic"]

   

]; 

     

main(); 

 

function main() { 

    try { // if something goes wrong in the try-catch block, the batch processor won't stop here. It will log the error message and continue further 

         

        var newFont, paragraphStyle, characterStyle, 

        doc = app.activeDocument, // The frontmost document 

        paragraphStyles = doc.allParagraphStyles, 

        characterStyles = doc.allCharacterStyles; 

         

     

 

        // Change in character styles 

        for (var c = 0; c < characterStyles.length; c++) { 

            characterStyle = characterStyles

            newFont = getNewFont(characterStyles.appliedFont + "\t" + characterStyles.fontStyle); 

            if (newFont != null) { 

                characterStyles.appliedFont = newFont; 

            } 

        } 

   

    // Change in paragraph styles 

        for (var p = 0; p < paragraphStyles.length; p++) { 

            paragraphStyle = paragraphStyles

            newFont = getNewFont(paragraphStyle.appliedFont.name); 

            if (newFont != null) { 

                paragraphStyle.appliedFont = newFont; 

            } 

        } 

         

        for (var i = 0; i < fontList.length; i++) { 

            var changed; 

            app.findTextPreferences = app.changeTextPreferences = NothingEnum.NOTHING; 

            app.findTextPreferences.appliedFont = fontList[0]; 

            app.changeTextPreferences.appliedFont = fontList[1]; 

            changed = doc.changeText(); 

            app.findTextPreferences = app.changeTextPreferences = NothingEnum.NOTHING; 

        } 

 

    } 

    catch(err) { // if an error occurs, catch it and write the  document's name, error message and line# where it happened into the log 

        gErrorLog.push(doc.name + " - " + err.message + ", line: " + err.line); 

    } 

 

function getNewFont(oldFontName) { 

    var newFont = null; 

     

    for (var p = 0; p < fontList.length; p++) { 

        if (oldFontName == fontList

[0]) { 

            newFont = app.fonts.itemByName(fontList

[1]); 

            if (!newFont.isValid) { 

                newFont = null; 

            } 

            break; 

        } 

    } 

 

    return newFont; 

daitranthanhoa
Inspiring
July 26, 2019

I'm sorry i was not more clear...

if i apply a paragraph style and then manually make a change within the paragraph


(override the paragraph style to make a small section within the paragraph be italic or bold)

the altered section will not be changed


I think your source can process.["Whitney (T1)\tRegular", "Whitney (OTF)\tRegular"]

If paragraph set font is : "Whitney (T1)\tRegular" by paragraph style.

you manually : set font style to italic

In your fontlist , you setting 2 fonts name:

["Whitney (T1)\tRegular", "Whitney (OTF)\tRegular"]

["Whitney (T1)\titalic", "Whitney (OTF)\titalic "]

Kasyan Servetsky
Braniac
July 17, 2019

Check out my 'change fonts scripts' for batch processor.

— Kas

Sunil Yadav
Braniac
July 17, 2019

You can try find and replace thing just like this:

/////============

var myDoc = app.documents[0]

app.findTextPreferences=app.changeTextPreferences= NothingEnum.nothing;

app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;

app.findTextPreferences.appliedFont = app.fonts.item("Times New Roman MT Std Bold");////Here you can specify your old font name

app.changeTextPreferences.appliedFont = app.fonts.item("Times New Roman Italic");////Here you can specify your NEW font name

myDoc.changeText();

/////============

Or else you can check out the link posted by Marijan Tompa (tomaxxi)

Best

Sunil