Skip to main content
Fightergator
Inspiring
April 21, 2025
Answered

Count Figures in Document

  • April 21, 2025
  • 3 replies
  • 762 views

I've written a script that provides a page and table count for each document and a total for a book.  I also want a count of all figures in each document. I tried the following, but counting graphic objects gives me a count that is many times more than the number of figures in the document.  

function countFigures() {
    figureCtr = 0;
    var figure = doc.FirstGraphicInDoc;
        while (figure.ObjectValid()) {
            figureCtr++;
            figure = figure.NextGraphicInDoc;
        }
           return figureCtr;
    }

 I've searched the forum for other examples, without success.  We work with relatively large books and these totals are useful in assessing the level of effort for each book.

Many thanks in advance...

    Correct answer frameexpert

    "magnificent_warmth" surprised me!  Happy you cleared that up.  Both answers were spot on...credit where credit is due! Turns out our figures use the "AFrame" constructor.name.  There's one other graphic object that is assigned the AFrame name occasionally; e.g., two times in a 500-page document. Maybe an old figure was deleted, but the frame still exists.  Either way, I can live with that.

     

    Hope all is going well and you got to enjoy the Easter season.


    Thanks! One thing to be aware of with loops like this: they traverse every object in the document, including thos on Master and Reference pages. So your "extra" frame may be on one of those pages.

    3 replies

    Community Expert
    April 22, 2025

    Hi,

    You could also count the number of figure tables (if you put your figures into tables) or the number of figure title paragraphs.

    Anchored frames can also be used for warning icons, etc. I do not know, if you want to count these as well.

    Best regards, Winfried

    Fightergator
    Inspiring
    April 23, 2025

    Now I see why it's so difficult (confusing) to count figures.  Unfortunately, I don't have any say over how the customer does their figures, but since they all appear to be AFrame, helps to narrow the search.  Am going to be out of town for a month (travel), but will take all your advice and see if I can track down the two extra figures in the one chapter when I get back.  Good news is it's a small chapter.

    Again, my deepest appreciation to everyone on the forum for all the great support!

    Bob_Niland
    Community Expert
    Community Expert
    April 22, 2025

    Figure, as opposed to Object, is an authoring-level page layout concept, typically a floating block of some sort.

    In FM, a single figure might be one Anchored Frame containing multiple imported objects.

    So an algorithm that enumerates imported objects might find that total to be different from the count of AFs. And for extra confusion, an AF can be empty (used to shove other content around, for example), and an import might be on a Master Page.

    This might or might not matter in your context.

    Participant
    April 21, 2025

    There are many different Graphic objects, including TextFrame, Line, Rectangle, etc. objects. So you have to test what kind you are looking for. If you want imported graphics, you look for Inset objects:

    if (figure.constructor.name === "Inset") {
        // Imported graphic.
    }
    frameexpert
    Community Expert
    Community Expert
    April 21, 2025

    I was logged in with a beta login and it gave me the magnicent_warmth handle. I want credit for the correct answer lol.

     

    There are many different Graphic objects, including TextFrame, Line, Rectangle, etc. objects. So you have to test what kind you are looking for.

    if (figure.constructor.name === "Inset") {
        // Imported graphic.
    }
    else if (figure.constructor.name === "AFrame") {
        // Anchored frame.
    }

     

    Fightergator
    Inspiring
    April 21, 2025

    "magnificent_warmth" surprised me!  Happy you cleared that up.  Both answers were spot on...credit where credit is due! Turns out our figures use the "AFrame" constructor.name.  There's one other graphic object that is assigned the AFrame name occasionally; e.g., two times in a 500-page document. Maybe an old figure was deleted, but the frame still exists.  Either way, I can live with that.

     

    Hope all is going well and you got to enjoy the Easter season.