Skip to main content
New Participant
December 1, 2017
Question

【Help Needed】Is there any script for changing font in CC 2017?

  • December 1, 2017
  • 2 replies
  • 3206 views

I tried to adjust the script below in cc 2017. But there are a lot of errors encoutered. The root cause I guess is the the script below was developed in 2010 which was using on a very old version of illustrator. Now, a bunch of the API, function in class don't exist any more.

//get unique Array elements

Array.prototype.unique = function (){

    var r = new Array();

    o:for(var i = 0, n = this.length; i < n; i++){

        for(var x = 0, y = r.length; x < y; x++){

            if(r==this) continue o;}

        r[r.length] = this;}

    return r;

}

//search inside array

Array.prototype.findIn = function(search){

    var r = Array();

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

        if (this.indexOf(search) != -1){

            r.push(this.substr(this.indexOf("\t") + 1, this.length));

        }

    return r;

}

var usedFonts = app.activeDocument.fonts;

var sysFonts = app.fonts.everyItem();

sysFontsList = sysFonts.fontFamily.unique();

sysFontsList.unshift("- Select Font Family -");

var fontsWin = new Window('dialog', 'Document Fonts');

fontsWin.alignChildren = 'center';

var allFonts = fontsWin.add('listbox', undefined, undefined, {numberOfColumns: 5, showHeaders: true, columnTitles: ['Font Name', 'Font Style', 'Font PS Name', 'Font Type', 'Chr count']});

allFonts.minimumSize = [600,150];

var myGrp = fontsWin.add('group');

myGrp.alignChildren = 'center';

var availableFonts = myGrp.add('dropdownlist',undefined,sysFontsList);

var availableStyles = myGrp.add('dropdownlist');

var okButton = myGrp.add('button',undefined,'OK');

var okButton = myGrp.add('button',undefined,'Cancel');

availableStyles.minimumSize = [180,25];

availableFonts.selection = 0;

availableFonts.onChange = function(){

    availableStyles.removeAll();

    var sysFontAvailableStyles = sysFonts.name.findIn(availableFonts.selection);

    for(var i = 0; i < sysFontAvailableStyles.length; i++)availableStyles.add('item',sysFontAvailableStyles);

    availableStyles.selection = 0;

}

//// SHOW JUST MISSING FONTS //// [START]

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

    if(usedFonts.status != FontStatus.INSTALLED){

        allFonts.add('item',usedFonts.fontFamily);

        allFonts.items[allFonts.items.length-1].image = 'SystemWarningIcon';

        allFonts.items[allFonts.items.length-1].subItems[3].text = findCharacters(usedFonts.name);

        allFonts.items[allFonts.items.length-1].subItems[0].text = usedFonts.fontStyleName;

        allFonts.items[allFonts.items.length-1].subItems[1].text = usedFonts.postscriptName;

        allFonts.items[allFonts.items.length-1].subItems[2].text = usedFonts.fontType;

    }

}

//// SHOW JUST MISSING FONTS //// [END]

allFonts.selection = 0;

fontsWin.center();

var fontAnswer = fontsWin.show();

if(fontAnswer == true && availableFonts.selection != 0){

    var sourceFont = allFonts.selection.text + "\t" + allFonts.selection.subItems[0].text;

    var destFont = availableFonts.selection.text + "\t" + availableStyles.selection.text;

    changeFont(sourceFont,destFont);

}else{

    alert('No Actions taken!');

}

function findCharacters(findFont){

    app.findGrepPreferences = NothingEnum.nothing;

    app.changeGrepPreferences = NothingEnum.nothing;

    app.findGrepPreferences.findWhat = ".";

    app.findGrepPreferences.appliedFont = findFont;

    var result = Number(app.activeDocument.findGrep().length);

   

    return result;

}

function changeFont(sourceFont, destinationFont){

    app.findTextPreferences = NothingEnum.nothing;

    app.changeTextPreferences = NothingEnum.nothing;

    app.findTextPreferences.appliedFont = sourceFont;

    app.changeTextPreferences.appliedFont = destinationFont;

    app.activeDocument.changeText();

}

This topic has been closed for replies.

2 replies

daneJ
Inspiring
December 5, 2017

So I am not really sure what the purpose of your script is mostly because i dont understand parts of it, nor clear what what problems you are having.

However judging from the title of this thread you want to change the font of text at some point in your script?

Like this?

var active_doc = app.activeDocument;

var text_frames = active_doc.textFrames; 

var afont =  textFonts[0]

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

     var this_text = text_frames.textRange;

      this_text.characterAttributes.textFont = afont     

}

And you want all the avaiable fonts?

the textFont Object has all the fonts available in Illustrator. I suppose you could loop through or what ever it is you need.

For fonts used in the document I suppose you could loop through all the textRanges and check the characterAttributes to get the font names.

I really dont know if any of this is what you are looking for.

Mike1987Author
New Participant
December 6, 2017

how can I get the fonts used in the document?

Mike1987Author
New Participant
December 6, 2017

Here is a loop that runs through all textFrames and gets the font used.

#target "illustrator"   

var active_doc = app.activeDocument;

var text_frames = active_doc.textFrames;

var usedFonts = []

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

      var this_text_frame = text_frames;

      usedFonts.push(this_text_frame.textRange.textFont.name)

      }

However this is only good if all text in a single textFrame has the same Font. If you have multiple fonts in the same text frame then you would have to make a loop to look through every character in the document seperately. Below is a quick example.

#target "illustrator"

var active_doc = app.activeDocument;

var text_frames = active_doc.textFrames;

var usedFonts = []

var listed = false

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

      var this_text_frame = text_frames;

          for (var t =0 ; t < this_text_frame.characters.length; t++){

               var aFont = this_text_frame.characters.textFont.name

               InArray(aFont)

              if(listed  == false){

                    usedFonts.push(aFont )

               }

           listed = false

           }

       }

function InArray(quiry){

for (var n =0 ; n < usedFonts.length; n++){

    var curFont = usedFonts

        if(curFont  == quiry){

            listed = true

        }

    }

return listed

}


Thank you Sir! It's really helpful. Last question, how can I replace font for a text frame?

I doesn't work for the following script

this_text.characterAttributes.textFont = "Gotham_bold"

Silly-V
Brainiac
December 1, 2017

Wow there used to be a document.fonts property? That would sure be useful!

I'm also interested in how this is solved.. I remember something about using metadata these days for getting the document fonts.

CarlosCanto
Community Expert
December 2, 2017

no there's never been a document.fonts property. Above script is for InDesign.

Jongware
Community Expert
December 2, 2017

Heh heh. Indeed it is. This has never ever worked for Illustrator – nor was it designed to.