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

Loop to know fonts used in a document Error in script

Engaged ,
Jan 23, 2023 Jan 23, 2023

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

TOPICS
Scripting
441
Translate
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

Community Expert , Jan 23, 2023 Jan 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)
}; 

 

Translate
Community Expert ,
Jan 23, 2023 Jan 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)
}; 

 

Translate
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 ,
Jan 23, 2023 Jan 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.

Translate
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 ,
Jan 24, 2023 Jan 24, 2023
LATEST

Thank you Peter.

Translate
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