Home > InDesign Scripts > QuickTip: Counting Text using everyItem()

QuickTip: Counting Text using everyItem()

Counting words and also paragraphs, stories, characters is something pretty easy to achieve, but very often done in wrong way. Most of the time, people are looping through stories, text frames, paragraphs and adding word count. That is OK, for short documents, with short stories, but as document length increases, script needs more and more time to loop through all stories, and get all words. People are forgetting about awesome method called everyItem! Method remained undocumented until InDesign CS3, but scripting experts such as Dave Saunders had already pointed out its forcefulness and already knew how to exploit it. It allows us to get all elements within collection. For more info about everyItem method be sure to check two awesome articles by Marc Autret: On ‘everyItem()’ – Part 1 and On ‘everyItem()’ – Part 2. So, let’s take a look how everyItem method can help us count stuff. :)


First, let’s take a look how to count words using loop:

var myDoc = app.activeDocument;
var myWords = 0;
for (var i = 0; myDoc.stories.length > i; i++)
    myWords += myDoc.stories[i].words.length

alert("Document contains " + myWords + " words.");

Now, let’s use everyItem method:

var myWords = app.activeDocument.stories.everyItem().words.length;

alert("Document contains " + myWords + " words.");

Isn’t it faster and easier? One row against four! Furthermore, we can use same technique to count paragraphs, characters, footnotes, and even text inside tables! Here are some examples:

var myStories = app.activeDocument.stories.everyItem();

// all paragraphs in document
var myParagraphs = myStories.paragraphs.length;

// all characters in document
var myCharacters = myStories.characters.length;

// all words inside footnotes
var myFootWords = myStories.footnotes.everyItem().words.length;

// all characters inside footnotes
var myFootCharacters = myStories.footnotes.everyItem().characters.length;

// all words inside tables
var myTableWords = myStories.tables.everyItem().cells.everyItem().words.length;

Next, we can also count text in selected text frame, or parent story of selected text frame:

var mySel = app.selection[0];

// all words inside selected text frame, even if frame is threaded
var mySelWords = mySel.texts.everyItem().words.length;

// all words inside selected text frame story
var mySelStoryWords = mySel.parentStory.words.length;
About these ads
  1. Gabberwouten
    January 13, 2011 at 09:44

    Hi,

    This seems very useful to me, but it doesn’t seem to work. When I try to run it, it just gives an error…

    Maybe it’s because i’m using the wrong file extensions?

    Since i’m not used to any kind of programming I can’t tell if this code is Java, Applescript or Visual Basic…

    Could anyone give me a hand, because I need to count the words and characters in a book we’re about to publish?

    Thanks!

    • January 13, 2011 at 12:28

      Hi!

      This is pure JavaScript, so extension must be “jsx”. If you want to count words in a book, you have to open each document and count words/characters because there is no way to access that data without opening files. Feel free to contact me if you need more help.

      Marijan.

      • Gabberwouten
        January 13, 2011 at 13:17

        Aha!

        Ok, thanks!

        I tried again, and no error this time, but also no result. I have the file open in InDesign and I can see the OSX ball spinning briefly after I run the script, but that’s it. Am I supposed to get a pop-up with the result?

  2. February 27, 2012 at 16:18

    Hey Gabberwout,
    No, you aren’t supposed to get a popup with the result. This scripts just counts and then stores the results in a variable. You could use an alert to show the contents of the variable.
    For example

    var mySel = app.selection[0];
    var mySelWords = mySel.texts.everyItem().words.length;
    alert (mySelWords);

    should do the trick. Good luck!

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 36 other followers

%d bloggers like this: