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

Loop to know fonts used in a document Error in script

Engaged ,
Jan 23, 2023 Jan 23, 2023

Copy link to clipboard

Copied

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

Views

192

Translate

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

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

 

Votes

Translate

Translate
Community Expert ,
Jan 23, 2023 Jan 23, 2023

Copy link to clipboard

Copied

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

 

Votes

Translate

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

Copy link to clipboard

Copied

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.

Votes

Translate

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

Copy link to clipboard

Copied

LATEST

Thank you Peter.

Votes

Translate

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