Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

A javascript for showing the name of used font.

Explorer ,
Apr 11, 2018 Apr 11, 2018

Copy link to clipboard

Copied

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.

TOPICS
Scripting

Views

3.0K
Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Explorer , Apr 12, 2018 Apr 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

...

Votes

Translate
Adobe
Community Expert ,
Apr 11, 2018 Apr 11, 2018

Copy link to clipboard

Copied

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.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 11, 2018 Apr 11, 2018

Copy link to clipboard

Copied

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

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 11, 2018 Apr 11, 2018

Copy link to clipboard

Copied

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

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Apr 12, 2018 Apr 12, 2018

Copy link to clipboard

Copied

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

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 12, 2018 Apr 12, 2018

Copy link to clipboard

Copied

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.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 12, 2018 Apr 12, 2018

Copy link to clipboard

Copied

You are on the right track, but you could save some conditional statements in there too if you use an object instead of an array, like so:

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;

        usedFonts[curFont] = 1;

    }

    for(prop in usedFonts)

    {

        resultString += prop + "\n";

    }

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

}

test();

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 12, 2018 Apr 12, 2018

Copy link to clipboard

Copied

Wow. That too worked. Thank-you.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 12, 2018 Apr 12, 2018

Copy link to clipboard

Copied

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

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Apr 16, 2018 Apr 16, 2018

Copy link to clipboard

Copied

LATEST

// 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.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 12, 2018 Apr 12, 2018

Copy link to clipboard

Copied

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;

}

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines