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

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

Inspiring
April 16, 2018

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


// load XMP Library

function loadXMPLibrary() { 

         if (!ExternalObject.AdobeXMPScript) { 

            try { 

                ExternalObject.AdobeXMPScript = new ExternalObject('lib:AdobeXMPScript'); 

            } catch (e) { 

              alert('Unable to load the AdobeXMPScript library!'); 

              return false; 

            } 

        } 

        return true; 

// unload XMP Library

function unloadXMPLibrary() { 

         if (ExternalObject.AdobeXMPScript) { 

            try { 

                ExternalObject.AdobeXMPScript.unload(); 

                ExternalObject.AdobeXMPScript = undefined; 

            } catch (e) { 

              alert('Unable to unload the AdobeXMPScript library!'); 

            } 

        } 

// list used fonts

function list_fonts () {

         var doc = app.activeDocument;

         var fontsInfo = []; 

         loadXMPLibrary (); 

         fontsInfo.push (getFontsInfo (doc.fullName)); 

         unloadXMPLibrary (); 

         var info = fontsInfo.join ('\n\n');

         return info;

}

// search used fonts

function getFontsInfo(file) {

        var arr = [], 

            xmpFile,

            oXmp,

            fontNumber,

            i,

            path,

            fontname,

            fonttype,

            ns = 'http://ns.adobe.com/xap/1.0/t/pg/'; 

        xmpFile = new XMPFile(file.fsName, XMPConst.UNKNOWN, XMPConst.OPEN_FOR_READ); 

        oXmp = xmpFile.getXMP();  

        fontNumber = oXmp.countArrayItems(ns, 'xmpTPg:Fonts'); 

        xmpFile.closeFile(XMPConst.CLOSE_UPDATE_SAFELY); 

        if (fontNumber) {  

            for (i = 1; i <= fontNumber; i++) { 

                path = XMPUtils.composeArrayItemPath(ns, 'xmpTPg:Fonts', i); 

                fontname = oXmp.getStructField(ns, path, XMPConst.TYPE_FONT, 'fontName'); 

                fonttype = oXmp.getStructField(ns, path, XMPConst.TYPE_FONT, 'fontType'); 

                arr.push([fontname, ' \t(', fonttype, ')'].join('')); 

            } 

        } 

        return arr.join('\n'); 

}

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

I tried to push it a little farther ahead:

This script also lists the type of font.

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.