adobe-indesignextendscript

InDesign: ExtendScript to list fonts and extended font information


I need to list detailed information about the fonts used in a set of inDesign documents. The information I need is essentially accessible through the menu item Type › Find Fonts… (as explained here) but going through each font in every document and writing down the information is not feasible.

I can find much of the information in the Font objects underdocument.fonts and my question is how to access or generate the extended properties found in the panel below:

1
(source: indesignsecrets.com)

Edit: The document.fonts array also doesn't seem to include missing fonts.


Solution

  • Well, here's a brute-force strategy for character counting. It iterates through every character textStyleRange in the document and checks its applied font. Edit: Updated to use textStyleRanges. Much faster than going through every character.

    var document = app.open(new File(Folder.desktop.fsName + "/test/test.indd"));
    try {
        var fontMultiset = countCharsInFonts(document);
    
        // For each font, display its character count.
        var fonts = document.fonts.everyItem().getElements();
        for (var i = 0; i < fonts.length; i++) {
            var fontName = fonts[i].fullName;
            $.writeln(fontName + ": " + fontMultiset[fontName]);
        }
    }
    finally {
        document.close();
    }
    
    function countCharsInFonts(document) {
        // Create the font multiset.
        var fontMultiset = {
            add: function add(fontName, number) {
                if (this.hasOwnProperty(fontName)) {
                    this[fontName] += number;
                }
                else {
                    this[fontName] = number;
                }
            },
        };
    
        // For every textStyleRange in the document, add its applied font to the multiset.
        var stories = document.stories.everyItem().getElements();
        for (var i = 0; i < stories.length; i++) {
            var story = stories[i];
            var textStyleRanges = story.textStyleRanges.everyItem().getElements();
            for (var j = 0; j < textStyleRanges.length; j++) {
                fontMultiset.add(textStyleRanges[j].appliedFont.fullName, textStyleRanges[j].length);
            }
        }
    
        // For any fonts that aren't applied in the document, set the character count to 0.
        var fonts = document.fonts.everyItem().getElements();
        for (var i = 0; i < fonts.length; i++) {
            var fontName = fonts[i].fullName;
            if (!fontMultiset.hasOwnProperty(fontName)) {
                fontMultiset[fontName] = 0;
            }
        }
    
        return fontMultiset;
    }