Skip to main content
rarur97798982
Inspiring
April 11, 2018
Answered

A javascript for showing the name of used font.

  • April 11, 2018
  • 3 replies
  • 3552 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
Legend
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();

rarur97798982
Inspiring
April 12, 2018

Can you please tell me, What is wrong here????

function test() 

     var docRef = app.activeDocument; 

     var frames = docRef.textFrames;

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

     { 

          if(frames.textRange.characterAttributes.textFont.name!="Times-Roman" || frames.textRange.characterAttributes.textFont.name!="Times-Italic" || frames.textRange.characterAttributes.textFont.name!="Times-Bold" || frames.textRange.characterAttributes.textFont.name!="Times-BoldItalic"){

              alert("Some texts are not Times-Roman, Times-Bold, Times-Italic, Times-Bold Italic")

          } else{

              alert("All texts are Times-Roman, Times-Bold, Times-Italic, Times-Bold Italic");

          }

     } 

test();

Mike_Gondek10189183
Community Expert
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.