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

Count Figures in Document

Contributor ,
Apr 21, 2025 Apr 21, 2025

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...

618
Translate
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 5 Correct answers

Community Beginner , Apr 21, 2025 Apr 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.
}
Translate
Community Expert , Apr 21, 2025 Apr 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.
}

 

Translate
Community Expert , Apr 21, 2025 Apr 21, 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.

Translate
Community Expert , Apr 22, 2025 Apr 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

Translate
Community Expert , Apr 22, 2025 Apr 22, 2025

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.

Translate
Community Beginner ,
Apr 21, 2025 Apr 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.
}
Translate
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 ,
Apr 21, 2025 Apr 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.
}

 

Translate
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
Contributor ,
Apr 21, 2025 Apr 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.

Translate
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 ,
Apr 22, 2025 Apr 22, 2025

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.

Translate
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 ,
Apr 22, 2025 Apr 22, 2025

Lol - I thought "magnicent_warmth" was sounding a lot like you Rick!

Translate
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 ,
Apr 22, 2025 Apr 22, 2025

Thank you @magnificent_warmth6396 , for all you do! 🙂

 

-Matt Sullivan
FrameMaker Course Creator, Author, Trainer, Consultant
Translate
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 ,
Apr 21, 2025 Apr 21, 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.

Translate
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 ,
Apr 22, 2025 Apr 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

Translate
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
Contributor ,
Apr 22, 2025 Apr 22, 2025
LATEST

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!

Translate
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