Skip to main content
Participant
October 22, 2019
Answered

InDesign AppleScript word count by page

  • October 22, 2019
  • 2 replies
  • 1219 views

I have a lengthy InDesign document and would love to get a word count of all the text on a page-level basis (i.e., page 1 has 156 words, page 2 has 190 words, etc.). I'm using AppleScript. 

 

The text is all in one story, so getting it by story doesn't help me. Each page begins with an occurence of a paragraph style I have called "Title."

 

Thanks for any help you are able to give!

    This topic has been closed for replies.
    Correct answer Sunil Yadav

    Hi Randolph_McMann,

     

    Try this code:

     

    tell application "Adobe InDesign CS6"
        set tempString to ""
        set allPages to pages of document 1
        repeat with i from 1 to count of allPages
            set allFrames to text frames of item i of allPages
            set tempString to tempString & "Total words on page " & i & " are :"
            set tempWordLength to 0
            repeat with j from 1 to count of allFrames
                set tempWordLength to tempWordLength + (count of words in item j of allFrames)
            end repeat
            set tempString to tempString & tempWordLength & "\n"
        end repeat
        display alert tempString
    end tell

     

     

    Best

    Sunil

    2 replies

    Community Expert
    October 23, 2019

    Hi Randolph,

    below some ExtendScript (JavaScript) code for all using InDesign on Windows and/or on Mac OS X.

    Run this code snippet when text is selected in the story. Could be a single insertion point as well.

     

    /*
    	WARNING:
    	1. NOT COUNTING words in table cells, words in footnote texts.
    	2. Not counting words if the text frame or the text path is on the pasteboard.
    	
    	Also see what exactly makes a word here:
    	
    	Highly recommended read, blog post by Marc Autret:
    	http://www.indiscripts.com/post/2011/09/what-exactly-is-a-word
    	
    */
    
    // SELECT SOME TEXT BEFORE RUNNING THIS:
    
    var story = app.selection[0].parentStory;
    var textContainers = story.textContainers;
    
    var resultArray = [];
    var resultString = "PagePos" +"\t"+ "Number of Words";
    
    for( var n=0; n<textContainers.length; n++ )
    {
    	if( textContainers[n].parentPage == null ){ continue };
    	var pagePositionString = ( textContainers[n].parentPage.documentOffset + 1 ) + "" ;
    	
    	if( resultArray[pagePositionString] == null )
    	{ resultArray[pagePositionString] = textContainers[n].words.length }
    	else
    	{
    		resultArray[pagePositionString] = 
    		resultArray[pagePositionString] + textContainers[n].words.length;
    	};
    };
    
    for( x in resultArray )
    {
    	resultString = resultString +"\r"+ x +"\t"+resultArray[x] ;
    };
    
    alert( resultString );

    Below  a sample result where a story is threaded from page 1 to the pasteboard, then on to page 2, page 4 and back to page 1 of a document. The text on the pasteboard will not be counted.

     

     

    Another note: To get word count with table cells and footnote text as well one could use e.g. GREP Find and limit the scope to text frames and text paths of a story. FWIW: The code above will also count text in frames that are nested inside e.g. groups.

     

    Regards,
    Uwe Laubender

    ( ACP )

    Participant
    October 24, 2019

    Thanks, Uwe! I hope my question has helped others. Thanks to your response and Sunil's, all the programming bases are covered.

    Sunil Yadav
    Sunil YadavCorrect answer
    Legend
    October 22, 2019

    Hi Randolph_McMann,

     

    Try this code:

     

    tell application "Adobe InDesign CS6"
        set tempString to ""
        set allPages to pages of document 1
        repeat with i from 1 to count of allPages
            set allFrames to text frames of item i of allPages
            set tempString to tempString & "Total words on page " & i & " are :"
            set tempWordLength to 0
            repeat with j from 1 to count of allFrames
                set tempWordLength to tempWordLength + (count of words in item j of allFrames)
            end repeat
            set tempString to tempString & tempWordLength & "\n"
        end repeat
        display alert tempString
    end tell

     

     

    Best

    Sunil

    Participant
    October 23, 2019

    Sunil,

     

    You're a genius. That seems to do just what I needed it to do. Thank you so much.

     

    (I would have responded sooner, but I did not get an email saying my post had been replied to, even though I clicked that option.)

     

    Randolph McMann