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

How to determine font type used in a textbox?

Guest
Feb 09, 2017 Feb 09, 2017

Copy link to clipboard

Copied

I'm trying to get the name of the font used in the text within a textbox using javascript, however all the help articles I see don't appear to work - I suspect that the functionality has been renamed or removed in recent versions of InDesign. (I'm using CC 2017.) Can anyone provide a code snippet that will work?

I've tried the following, based on another post:

var docPages = app.activeDocument.pages; 

var numPages = docPages.length; 

for (i = 0; i < numPages; i++) {

     pageItems = docPages.allPageItems;

     for (j = 0; j < pageItems.length; j++) {

          if currentItem instanceof TextFrame) {

            // try to get font used in TextFrame

               fontName = pageItems.paragraphs[0].fontStyle; // DOESN'T WORK

          }

     }

}

I don't see any reference to anything that lets me query the contents of the textbox on the TextBox API page.

Adobe InDesign CS6 (8.0) Object Model JS: TextBox

Any advice would be greatly appreciated!

TOPICS
Scripting

Views

981

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
Guest
Feb 09, 2017 Feb 09, 2017

Copy link to clipboard

Copied

Figured this out now:

for (j = 0; j < pageItems.length; j++) {

     currentItem = pageItems

          if (currentItem instanceof TextFrame) { 

            // try to get font used in TextFrame 

               if (currentItem.contents.length > 0) {

                   $.writeln(currentItem.paragraphs[0].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 ,
Feb 09, 2017 Feb 09, 2017

Copy link to clipboard

Copied

LATEST

Hi,

the objects textFrame and textBox are two very different objects.

textBox is one of the formField objects and has very limited formatting options.

So textFrame is obviously the one you were looking for.

However the applied font you are reading out with paragraphs[0].appliedFont.name will only reflect the formatting of the first character in the first paragraph of the frame (if a paragraph is existing at all).

To get the appliedFont information for the whole text frame you better hunt for textStyleRanges and inspect them.

See the following example:

MixedFontsInFirstParagraph.png

Your code would one give one result that is reflecting the formatting of the first character:

Mistral    Regular

Assume the text frame shown above is selected
and you are working with textStyleRanges the result would be more accurat:

var uniqueNamesArray = [];

var orderInAppearanceNamesArray = [];

var usedFontNames = [];

var textFrame = app.selection[0];

var x;

var textStyleRangesArray = textFrame.texts[0].textStyleRanges.everyItem().getElements();

for(var n=0;n<textStyleRangesArray.length;n++)

{

     var fontName = textStyleRangesArray.appliedFont.name;

     // Do a unique list:

     uniqueNamesArray[fontName] = fontName;

     orderInAppearanceNamesArray = fontName;

};

// Reading out the uniqueNamesArray:

$.writeln("uniqueNamesArray:");

for(x in uniqueNamesArray)

{

     $.writeln(x);

};

// Reading out the orderInAppearanceNamesArray:

$.writeln("\r"+"orderInAppearanceNamesArray:");

for(var n=0;n<orderInAppearanceNamesArray.length;n++)

{

    $.writeln(n+"\t"+orderInAppearanceNamesArray);

}

The result will be:

uniqueNamesArray:

Mistral    Regular

Adobe Garamond Pro    Regular

Minion Pro    Regular

Verdana    Bold

orderInAppearanceNamesArray:

0    Mistral    Regular

1    Adobe Garamond Pro    Regular

2    Minion Pro    Regular

3    Verdana    Bold

4    Minion Pro    Regular

5    Mistral    Regular

6    Minion Pro    Regular

And then we have the collection: document.fonts

var docFonts = app.documents[0].fonts.everyItem().getElements();

for(var n=0;n<docFonts.length;n++)

{

    $.writeln(n+"\t"+docFonts.name)

};

That will give you also the fonts used in empty text frames, in fact the font used with the single insertion point of an otherwise empty story.

I added an empty text frame to the document and formatted it with a character style that is using another font, Arno Pro:

EmptyTextFrameAdded.png

So the result with my snippet using document.fonts will be:

0    Arno Pro    Regular

1    Verdana    Bold

2    Minion Pro    Regular

3    Adobe Garamond Pro    Regular

4    Mistral    Regular

Regards,
Uwe

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