Skip to main content
Inspiring
January 23, 2023
Answered

Loop to know fonts used in a document Error in script

  • January 23, 2023
  • 2 replies
  • 410 views

Error in this line: var fontName = textFrames[i].textRange.characterAttributes.textFont.name;

 

Here is the code:
var textFrames = app.activeDocument.textFrames;

// Create an object to store
var usedFonts = {};

// Loop through all text frames
for (var i = 0; i < textFrames.length; i++) {
var fontName = textFrames[i].textRange.characterAttributes.textFont.name;
if (!usedFonts[fontName]) {
usedFonts[fontName] = true;
}
}


for (var font in usedFonts) {
$.writeln(font);
}

This topic has been closed for replies.
Correct answer rob day

Hi @virender_CTS , A textFrame has no textRange property—try textStyleRanges:

 

 

//gets the fons used in the document’s first text frame
var tr = app.activeDocument.textFrames[0].textStyleRanges;

for (var i = 0; i < tr.length; i++){
    $.writeln(tr[i].appliedFont.name)
}; 

 

2 replies

Peter Kahrel
Community Expert
Community Expert
January 23, 2023

In addition to Rob's suggestion, you can also use this:

 

$.writeln (app.activeDocument.fonts.everyItem().name.join('\r'));

 

An advantage of this approach is that every font name is returned just once.

 

Note that in both Rob's and my approach you get only the fonts that are used in text. Fonts in unused character and paragraph styles aren't reported.

 

P.

Inspiring
January 24, 2023

Thank you Peter.

rob day
Community Expert
rob dayCommunity ExpertCorrect answer
Community Expert
January 23, 2023

Hi @virender_CTS , A textFrame has no textRange property—try textStyleRanges:

 

 

//gets the fons used in the document’s first text frame
var tr = app.activeDocument.textFrames[0].textStyleRanges;

for (var i = 0; i < tr.length; i++){
    $.writeln(tr[i].appliedFont.name)
};