Skip to main content
rarur97798982
Inspiring
April 11, 2018
Answered

A javascript for showing the name of used font.

  • April 11, 2018
  • 3 replies
  • 3513 views

Hi,
I need to show the name of font which is used in the active document. I am new in scripting. I don't get any clue for this.

doc = activeDocument;

texts = doc.textFrames;

count = texts.length;

for (var i = 0; i < count; i++ ) {

    textRef = texts;

    textRef.selected = true

var f=textRef.Font ; 

alert(f);

Every-time, I am getting "undefined". Can Anyone give me what is wrong in this code? Thanks in advance.

This topic has been closed for replies.
Correct answer rarur97798982

This is how it got fixed.
Thanks to all those who helped me.

function test()

{    

    var docRef = app.activeDocument;

    var usedFonts =[];

    var frames = docRef.textFrames;

    var len=frames.length;

    var dummy="";

    for(var x=0;x<len;x++)    

    {

        usedFonts.push((frames.textRange.characterAttributes.textFont.name));

    }

    alert("Fonts used in this document: \n"+usedFonts.unique());

}  

test();  

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;

}

3 replies

rarur97798982
rarur97798982AuthorCorrect answer
Inspiring
April 12, 2018

This is how it got fixed.
Thanks to all those who helped me.

function test()

{    

    var docRef = app.activeDocument;

    var usedFonts =[];

    var frames = docRef.textFrames;

    var len=frames.length;

    var dummy="";

    for(var x=0;x<len;x++)    

    {

        usedFonts.push((frames.textRange.characterAttributes.textFont.name));

    }

    alert("Fonts used in this document: \n"+usedFonts.unique());

}  

test();  

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;

}

Disposition_Dev
Community Expert
April 11, 2018

"Font" isn't a property of the textFrame object.

Additionally it looks like you're trying to alert the font of each text frame in the document, however you're not alerting until after the loop is complete. So each time the loop runs, you're overwriting the "f" variable, so as it stands, you'll only ever get the font for the last textFrame.

In order to get the font from a given textFrame, you need to check the textRange.characterAttributes.textFront property. Give the following a try:

function test()

{

     var docRef = app.activeDocument;

     var frames = docRef.textFrames;

     for(var x=0,len=frames.length;x<len;x++)

     {

          alert(frames.textRange.characterAttributes.textFont.name);

     }

}

test();

Inspiring
April 12, 2018

I would suggest using an array to gather the font info. After that you can search the array if any font not being Times and give a result.

function test() 
     var docRef = app.activeDocument;
     var usedFonts =[];
     var frames = docRef.textFrames; 
      
     for(var x=0,len=frames.length;x<len;x++) 
    
          usedFonts.push((frames.textRange.characterAttributes.textFont.name)); 
    
usedFonts = usedFonts.join ("\n")
alert ("Fonts used in this document: \n" + usedFonts);
test(); 

Disposition_Dev
Community Expert
April 12, 2018

Hey, Thanks for the answer. I had changed something. But, my illustrator is not showing any result.

function test()

{

    var docRef = app.activeDocument;

    var usedFonts =[];

    var frames = docRef.textFrames;

    var len=frames.length;

    for(var x=0;x<len;x++)

    {

        if(usedFonts.length==0){

            usedFonts.push((frames.textRange.characterAttributes.textFont.name));

        }else{

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

                if(usedFonts==frames.textRange.characterAttributes.textFont.name){

                }else{

                    usedFonts.push((frames.textRange.characterAttributes.textFont.name));

                }

          }

        }

    }

    alert ("Fonts used in this document: \n" + usedFonts);

}

test();


So that it will not show same fonts again.


Here's an even better and slightly more efficient version of my previous suggestion. I removed the second loop in favor of a simple condition inside the first loop.

function test()

{

    var docRef = app.activeDocument;

    var usedFonts = {};

    var resultString = "";

    var frames = docRef.textFrames;

    var curFrame,curFont;

    var len = frames.length;

    for (var x = 0; x < len; x++)

    {

        curFrame = frames;

        curFont = curFrame.textRange.characterAttributes.textFont.name;

        if(!usedFonts[curFont])

        {

            resultString += curFont + "\n";

            usedFonts[curFont] = 1;

        }

    }

    alert("Fonts used in this document: \n" + resultString);

}

test();

Mike_Gondek10189183
Community Expert
April 11, 2018

We have a forum for that where you will more likely  get an answer

Illustrator Scripting

I, or someone else with admin rights on forum, can move your post there if you like, just let us know.