Skip to main content
Participating Frequently
November 8, 2010
Question

JSX for word count, including tables

  • November 8, 2010
  • 1 reply
  • 1784 views

Hi folks,

I've tried two different scripts (found on this site) to attempt to get the word count out of an entire book, including all stories, tables, and captions.

The first:

alert("There are "+ app.documents[0].stories.everyItem().words.length +" words in the active document.")

Then this one, which doesn't count numbers and symbols, which I like:

app.findGrepPreferences = null;

app.findGrepPreferences.findWhat = "[\\u\\l][-\\u\\l]+";

found = app.documents[0].findGrep ();

alert ("There are " + found.length + " words in this document.")

The difficulty is that neither of them include words in tables. Is there a way to force this in this script?

This topic has been closed for replies.

1 reply

Peter Kahrel
Community Expert
Community Expert
November 10, 2010

// First do a word count in the main text (as before)

found = app.documents[0].findGrep ().length;

// Then find every word in every table in every story

t = app.documents[0].stories.everyItem().tables.everyItem().findGrep();

// "t" is an array of arrays, now you need to add the length of each array to "found"


for (i = 0; i < t.length; i++)
   found += t.length;

alert ("There are " + found + " words in this document.")

Peter

Participating Frequently
November 10, 2010

As written, it threw an error. When I added script from one of my other ones, it did, but I am still not getting the correct answer. It now looks like this.

// First do a word count in the main text (as before)


app.findGrepPreferences = null;

app.findGrepPreferences.findWhat = "[\\u\\l][-\\u\\l]+";

found = app.documents[0].findGrep ().length;

// Then find every word in every table in every story

t = app.documents[0].stories.everyItem().tables.everyItem().findGrep();

// "t" is an array of arrays, now you need to add the length of each array to "found"


for (i = 0; i < t.length; i++)

   found += t.length;

alert ("There are " + found + " words in this document.")

It somehow manages to give a word count lower than the one which skips tables, and is over 1000 less than our "official" count from our translation company.

Peter Kahrel
Community Expert
Community Expert
November 10, 2010

>It somehow manages to give a word count lower than the one which skips tables

That's bizarre. It doesn't do any subtractions, so how the total can be lower if you include tavles is beyond me.

>and is over 1000 less than our "official" count from our translation company.

What does "official count" mean?

Petr