0

I'm writing a javascript program to handle each character intо Indesign document.

To begin with, I wrote two different ways of counting characters, which for some reason give different results for large documents. Why?

            var  
            myDocument,  docStories,  docCharacters,
            docFootnotesCharacters,  docTablesCharacters; 

            myDocument = app.activeDocument;

            var TotalChars = 0;

            // Fisrt way
            docStories = myDocument.stories.everyItem();
            docCharacters = docStories.characters.length;  
            docFootnotesCharacters = docStories.footnotes.everyItem().characters.length;  
            docTablesCharacters = docStories.tables.everyItem().cells.everyItem().characters.length;  

             statReport = []; 

        // Second way     
        for ( j = 0; j < myDocument.stories.length; j++ ) {
            myStory = myDocument.stories.item(j);
            var Frames = myStory.textContainers;
            for ( i = 0; i < Frames.length; i++ ) {

                var Frame = Frames[i];
                for (var TextCnt = 0; TextCnt < Frame.texts.length; TextCnt++) {
                    CurrentText = Frame.texts.item(TextCnt);
                    TotalChars += CurrentText.characters.length;
                }
                for (var TableCnt = 0; TableCnt < Frame.tables.length; TableCnt++) {
                    var CurrentTable = Frame.tables.item(0);
                    for ( var CellCnt = 0; CellCnt < CurrentTable.cells.length; CellCnt++ ) {
                        var CurrentCell = CurrentTable.cells.item(CellCnt);
                        TotalChars += CurrentCell.characters.length;
                    }
                }
                for (var FootNoteCnt = 0; FootNoteCnt < Frame.footnotes.length; FootNoteCnt++) {
                    var CurrentFootNote = Frame.footnotes.item(0);
                    TotalChars += CurrentFootNote.characters.length; 
                }
            }
        }
        statReport.push ( "Characters: " + ( docCharacters + docFootnotesCharacters + docTablesCharacters ) ); 
        statReport.push ( "TotalChars: " + TotalChars ); 
        alert ( statReport.join ( "\r" ), "Document Text Statistic" );  
Jongware
  • 22,200
  • 8
  • 54
  • 100
nkp07
  • 1

1 Answers1

2

In the second method, you're counting all the characters inside text frames in the story. But stories can be overset (the text overflows). The first method will count overset text (because you're counting the characters in a story, but the second method will ignore those, because it's only counting characters in story frames.

Ariel
  • 119
  • 1
  • 10
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - [From Review](/review/low-quality-posts/21813444) – Michel Dec 31 '18 at 16:16
  • 2
    Of course it answers the question. The question was why do the 2 methods give different answers. The answer is precisely as stated. – Ariel Jan 01 '19 at 01:53