Thursday, October 13, 2011

Counting Paragraphs in a Document

You may have considering counting words but who wants to counts paragraphs? Perhaps no one. But to count paragraphs, you have to find them all. Therefore counting paragraphs provides a simple exercise in traversing a list. Once the paragraphs are counted/found, something more interesting can be learned about them or done to them.

Note:  You can access a word counting utility from the File>Utilities>Document Reports command. This utility is implemented as an FDK plug-in.

Start by getting the active document and by setting the count variable to 0. Next get the first paragraph in the list of unordered paragraph. It is a document property. If you start typing from memory, the ESTK nicely prompts you with the possible completions. (NICE!)

The scripts calls for a while loop. Stop when Frame runs out of paragraphs. Here is where the FDK programmer in me gets into trouble. I am tempted to write while (pgf) assuming that pgf goes to zero when there are no more. That does not hold in ExtendScript. (If you goof like I did you will end up with FrameMaker in an infinite loop. At that point kill it using the Windows Task Manager.)

while (pgf.ObjectIsValid()) determines whether the object exists in the current document.

Each time through the loop the count is incremented, the attempts to find a next paragraph. Once again, the auto-completion feature in the ESTK is helpful.

Now all that remains is to report the count.

count = count+''; //concatendate empty string to convert

The completed script is shown here:

var doc = app.ActiveDoc;
var count = 0;

var pgf = doc.FirstPgfInDoc;
while (pgf.ObjectValid()) {
    pgf = pgf.NextPgfInDoc;

count = count+''; //Concatenate empty string to convert

 If you run this script with the Portrait template, you get the following:

Think of all of those master and reference page paragraphs and you will understand how the number got to be so large.

No comments:

Post a Comment