Reply To: Calculation of Paragraph length(s) in a document

#82984
Peter Kahrel
Participant

Maybe existing style utilities can determine paragraph length globally. As far as I can see, InTools’s style utility doesn’t, but a basic version is not difficult to script. The example below creates a text file Paragraph_count.txt on your desktop with the name of each document followed by a list of stories, each with a list of paragraph index and word count. The file is opened after script finishes.

I said ‘basic’ because, well, it’s basic. It doesn’t do footnotes or tables, it includes stories on master pages, and you get just a raw count. And that’s the reason why general utilities tend not to include what you want: there are endless ways to customise the presentation of the data, so the script you’re after will typically be a custom job.

(function () {
  
  var i, j;
  var report;
  var stories, par;
  var f;
  
  report = [];
  for (i = 0; i < app.documents.length; i++) {
    report.push (app.documents[i].name);
    stories = app.documents[i].stories.everyItem().getElements();
    for (j = 0; j < stories.length; j++) {
      report.push ('Story ' + stories[j].id);
      par = stories[j].paragraphs.everyItem().getElements();
      for (k = 0; k < par.length; k++) {
        report.push ('Par. ' + k + ':' + par[k].words.length);
      }
    }
  }

  f = File ('~/Desktop/Paragraph_count.txt');
  f.encoding = 'UTF-8';
  f.open('w');
  f.write(report.join('\r'));
  f.close();
  f.execute();
}());

This article was last modified on March 17, 2016

Comments (0)

Loading comments...