2 Replies Latest reply on Feb 9, 2017 11:01 PM by Laubender

    How to determine font type used in a textbox?

    printastic_dev

      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[i].allPageItems;
           for (j = 0; j < pageItems.length; j++) {
                if currentItem instanceof TextFrame) {
                  // try to get font used in TextFrame
                     fontName = pageItems[j].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!

        • 1. Re: How to determine font type used in a textbox?
          printastic_dev Level 1

          Figured this out now:

           

          for (j = 0; j < pageItems.length; j++) {
               currentItem = pageItems[j];  
                    if (currentItem instanceof TextFrame) {  
                      // try to get font used in TextFrame   
                         if (currentItem.contents.length > 0) {
                             $.writeln(currentItem.paragraphs[0].appliedFont.name);
                         }
                    }  
          }  
          
          • 2. Re: How to determine font type used in a textbox?
            Laubender Adobe Community Professional & MVP

            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[n].appliedFont.name;
                 // Do a unique list:
                 uniqueNamesArray[fontName] = fontName;
                 orderInAppearanceNamesArray[n] = 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[n]);
            }
            

             

            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[n].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