Skip to main content
Known Participant
October 23, 2012
Answered

[JS][CS4]List fonts used in the Document

  • October 23, 2012
  • 1 reply
  • 5215 views

Hi to all scripting gurus,

I have this following script that returns a list of fonts used in the document:

function fontused(){        

      var fontscoll = document.fonts;         

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

          var font = fontscoll;            

          alert(font.name);          

     }

}

The above is working OK my problem is that it also list the fonts used in the placed illustrator files (.eps). Is there some kind of parameter or something that i can use so that it only list the fonts used in the text?

-CharlesD

This topic has been closed for replies.
Correct answer Trevor:

Hi Charles

Try this

var  myDocFonts=app.documents[0].stories.everyItem().textStyleRanges.everyItem().appliedFont,

        l = myDocFonts.length, myFonts="";

        uniqueFonts = {}, n;

while (l--) uniqueFonts[myDocFonts.name] = myDocFonts.name;

for (n in uniqueFonts) myFonts+=(uniqueFonts)+"\r";

alert(myFonts)

Trevor

1 reply

Trevor:
Trevor:Correct answer
Legend
October 23, 2012

Hi Charles

Try this

var  myDocFonts=app.documents[0].stories.everyItem().textStyleRanges.everyItem().appliedFont,

        l = myDocFonts.length, myFonts="";

        uniqueFonts = {}, n;

while (l--) uniqueFonts[myDocFonts.name] = myDocFonts.name;

for (n in uniqueFonts) myFonts+=(uniqueFonts)+"\r";

alert(myFonts)

Trevor

Known Participant
October 24, 2012

Hi Trevor,

Thanks for this. I had to delete: ", n" coz its returning an error string: n is undefined. Its running OK now. Any thoughts on why i had to delete that part?

Trevor:
Legend
October 24, 2012

Hi Charles,

The ",n" caused an error because of the ";" "after myFonts=""" the ";" there should have been a "," as I meant to declare all the variables from "l" to "n" as a var.  By having the ";" the "uniqueFonts = {}, n;" becomes detached from the var and the statement about the "n" becomes completely meaningless. The equivalent of having "Hello there" written in the middle of the script, thus throwing an error. It was a typo, sorry about that

Either way Glad to help,

Trevor