ExtendScript to print the word count in an FM book file

Find the script here.
To test the script:

  1. Create a book file with many chapters. Chapters must be added as files.
  2. Open the book file.
  3. Ensure that chapters are not opened.
  4. Run the script.
  5. Wait for the script to open all the chapters and then print the word count.
    (Note that the script doesn’t include the words inserted in tables.)

6 Responses to ExtendScript to print the word count in an FM book file

  1. Milo Grika says:

    When I run the script, it opens the first chapter then raises the following error:

    Script Error :
    Error Message : FTI_String is undefined
    Script, Line# : C:\Users\mgrika\Documents\findWordCountBook.jsx, 44

  2. sbhaskar says:

    @Milo,
    Are you using FrameMaker 10?
    Sorry for the delay in response. Was stuck with some work, and just catching up. :)

  3. Joseph Lorenzini says:

    Yes, there is a bug in this script. The issue this line:

    var t1=flow.GetText(FTI_String);

    The intention is to use the GetText method to obtain all the strings in the document. However, to do that you must pass a constant to the method. In this case, the constant is Constants.FTI_String. You are missing ‘Constants’. If you add that in like so:

    var t1=flow.GetText(Constants.FTI_String);

    Then, the script should work.

    With all due respect, it would be really nice if Adobe could at least test the example scripts once before sending them out. This could have been caught with maybe a minute or two of debugging since the error created an undefined object.

    • sbhaskar says:

      Thanks for the feedback, Joseph.
      The script was not throwing any error in machine when I tested. Thanks for trying it out.

      Sunil

  4. Joseph Lorenzini says:

    there’s also another problem in the scrip.

    When you deference the text array, you use this replace method to remove the trailing whitespace:

    var str=t1[i] .sdata.replace(/^\s+|\s+$/g, ”)

    And then you use this method to count the white spaces that remain:

    no=no+str.split(” “).length;

    The assumption is that the number of whitespaces should equal the numer of words. And that is true in many cases. however, what if there were two words with multiple tabs or spaces in between. Then that would mean each space would be counted as a word. To address this problem, you need to find and replace multiple spaces with a single space. Here’s one way to do this:

    var str=t1[i] .sdata.replace(/^\s+|\s+$/g, ”) ;
    var str2=str.replace(/\s+/g, ‘ ‘) ;
    no=no+str2.split(” “).length;