26 Replies Latest reply on Jan 27, 2012 12:02 AM by Laubender

    how to get character count

    RockSel

      Hi All,

       

      How to read the below highlighted value in script. Pl help me.

       

      Regards,

      RockSelFindValue.JPG

        • 1. Re: how to get character count
          csm_phil Level 4

          Hi RockSel,

           

          Please try the below JS script is alert the character count. Please before select the text after run the script.

           

           

          try{
              var mySel = app.selection[0].characters.length;
              alert("Characters count : "+mySel);    
              }
          catch(e){
              alert("Please select the texts!");
              }
          

           

          thx

          csm_phil

          • 2. Re: how to get character count
            கற்பனை (Imagine) Level 3

            try{
                var mySel = app.selection[0].characters.length-1;
                alert("Characters count : "+mySel);   
                }
            catch(e){
                alert("Please select the texts!");
                }

             

            length -1 will shown the exact UI values, because js index starts from zero.

            • 3. Re: how to get character count
              RockSel Level 1

              Ok Thank you so much.

               

              How to select whole document with out master pages.

              • 4. Re: how to get character count
                csm_phil Level 4

                Hi Rock Sel,

                 

                Try the below js code.

                 

                 

                var myDoc = app.activeDocument.pages.everyItem().textFrames.everyItem();
                try{
                    var myCharlength = myDoc.characters.length;
                    }catch(e){}
                if(myCharlength<=0){
                    alert("Characters count is 0 in this document!");
                    }
                else{
                    alert("Characters count : "+myCharlength);    
                    }
                

                 

                 

                thx

                csm_phil

                • 5. Re: how to get character count
                  Laubender Adobe Community Professional & MVP

                  @csm_phil:
                  in that case you will not get the contents of tables or footnotes. To be precise: you'll get the (one) special character that indicates a table, but not the table contents and you'll get the (one) special character that indicates a foot note.

                   

                  So you have to iterate through all tables and footnotes to get the length of its contents, too.

                   

                  For tables, where the contents is an array, it's best to look after:

                   

                  _allTables[i].contents.join("").length

                   

                  And of course you will count all paragraph signs, all newline signs and all table indicators as characters…
                  Not to forget tables inside tables, tables inside footnotes…

                   

                  Uwe

                  • 6. Re: how to get character count
                    AnthonyMasure

                    Hi Laubender,

                     

                    Can you give us the character counter code who include table and footnotes for all stories ?

                    Does it include white spaces ?

                     

                    I won't be able to find it elsewhere.

                     

                    Thanks a lot,

                    Anthony

                    • 7. Re: how to get character count
                      Marijan Tompa [tomaxxi] Level 4

                      Anthony,

                       

                      I suggest you read this article:

                      http://www.indiscripts.com/post/2011/09/what-exactly-is-a-word

                       

                      Hope that helps.

                       

                      --

                      Marijan (tomaxxi)

                      http://tomaxxi.com

                      • 8. Re: how to get character count
                        AnthonyMasure Level 1

                        Thanks, seems good for adding content, but I'm unable to fork it for characters...

                        • 10. Re: how to get character count
                          AnthonyMasure Level 1

                          First exemple (Word counter) works great, but I didn't succeed with footnotes, etc.

                          You should propose a complete script like this in your picture for noobs like me

                           

                          Thanks a lot,

                          Anthony

                          • 11. Re: how to get character count
                            Marijan Tompa [tomaxxi] Level 4

                            Something like this?

                             

                            if ( app.documents.length > 0 ) {
                                if ( app.activeDocument.stories.length > 0 ) {
                                    var
                                        myDocument = app.activeDocument,
                                        docStories = myDocument.stories.everyItem(),
                                        docCharacters = docStories.characters.length,
                                        docWords = docStories.words.length,
                                        docFootnotesCharacters = docStories.footnotes.everyItem().characters.length,
                                        docFootnotesWords = docStories.footnotes.everyItem().words.length,
                                        docTablesCharacters = docStories.tables.everyItem().cells.everyItem().characters.length,
                                        docTablesWords = docStories.tables.everyItem().cells.everyItem().words.length
                                        statReport = [];
                                        
                                        statReport.push ( "Document text statistic:" );
                                        statReport.push ( "===================" );
                                        statReport.push ( "" );
                                        statReport.push ( "[Stories]");
                                        statReport.push ( "------------------------------" );
                                        statReport.push ( "Characters: " + docCharacters );
                                        statReport.push ( "Words: " + docWords );
                                        statReport.push ( "" );
                                        statReport.push ( "[Footnotes]");
                                        statReport.push ( "------------------------------" );
                                        statReport.push ( "Characters: " + docFootnotesCharacters );
                                        statReport.push ( "Words: " + docFootnotesWords );
                                        statReport.push ( "" );
                                        statReport.push ( "[Tables]");
                                        statReport.push ( "------------------------------" );
                                        statReport.push ( "Characters: " + docTablesCharacters );
                                        statReport.push ( "Words: " + docTablesWords );
                                        statReport.push ( "" );
                                        statReport.push ( "[TOTAL]");
                                        statReport.push ( "===================" );
                                        statReport.push ( "Characters: " + ( docCharacters + docFootnotesCharacters + docTablesCharacters ) );
                                        statReport.push ( "Words: " + ( docWords + docFootnotesWords + docTablesWords ) );
                                        
                                        alert ( statReport.join ( "\r" ), "Document Text Statistic" );
                                } else {
                                    alert ( "No stories found in the document!", "Document Text Statistic" );
                                }
                            } else {
                                alert ( "No opened document!", "Document Text Statistic" );
                            }
                            

                             

                            Hope that helps.

                             

                            --

                            Marijan (tomaxxi)

                            1 person found this helpful
                            • 12. Re: how to get character count
                              AnthonyMasure Level 1

                              Seems good, but I get an error in InD 5.5 (line 10) : "docStories.tables.everyItem().cells.everyItem is not a fonction"

                              Sorry about the time you took for me.

                              Can you also tell me if blank characters  (spaces) are counted ? Just to know.

                               

                              Edit : If I replace the variable by 0 it works for the others (of course, tables are not counted)

                               

                              /a.

                              • 13. Re: how to get character count
                                Marijan Tompa [tomaxxi] Level 4

                                There is a missing comma at the end of the docTablesWords line.

                                BTW, it's working just fine for me here. (ID CS5.5)

                                1 person found this helpful
                                • 14. Re: how to get character count
                                  AnthonyMasure Level 1

                                  Strange, not here even with the missing comma, but hoppefully I didn't use tables

                                   

                                  Can you also tell me if it's possible to exclude blocks outside the limits of the pages (outside the black frame), and/or blank spaces ?

                                  If it's too complicated, please don't waste your time.

                                   

                                  Your "total" function is nice

                                  • 15. Re: how to get character count
                                    Marijan Tompa [tomaxxi] Level 4

                                    Ahh, I'm too tired.

                                    Use this two lines for tables:

                                     

                                    docTablesCharacters = ( docStories.tables.length > 0 ? docStories.tables.everyItem().cells.everyItem().characters.length : 0 ),
                                    docTablesWords = ( docStories.tables.length > 0 ? docStories.tables.everyItem().cells.everyItem().words.length : 0 ),
                                    

                                     

                                    There is no fast and easy way to exclude items on pasteboard from counting.

                                    If you want to do it, you have to loop through stories and check where the parent text frame is.

                                    Maybe someone else has script like that.

                                     

                                    --

                                    Marijan (tomaxxi)

                                    1 person found this helpful
                                    • 16. Re: how to get character count
                                      AnthonyMasure Level 1

                                      No error message with these lines, I can't try the table counter because I haven't (yet) tables, but I suppose it works.

                                      Thank you very much for helping, for now my question is "answered" (kitch smiley)

                                       

                                      Kind regards,

                                      Anthony (Paris)

                                      • 17. Re: how to get character count
                                        Loic.Aigon Adobe Community Professional

                                        To keep on this topic,

                                         

                                        Regarding to spaces not to be taken into account, my 2cts thought is to call F/C on every story to get count of characters only.

                                        app.findGrepPreferences.findWhat = "[^\\s]+"; //Look for everything but spaces of any kind

                                        var found = docStories.findGrep(); //Find all occurencs

                                        alert(found[0].length); //which would discard spaces characters

                                         

                                        But it's a raw approach that may be clunky.

                                        BTW, I expected that the array of results would be [ result, result... ] a,d looks like it's [ [ result, result... ] ] (inner array).

                                         

                                        Hope it helps,

                                         

                                        Loic

                                        • 18. Re: how to get character count
                                          Laubender Adobe Community Professional & MVP

                                          @Anthony – for "white space": yes, every character or special character is included in the count. Not only "white space", but returns, soft returns, brakes, special table, special note or footnote characters etc.pp.

                                           

                                          Example:
                                          You have a document with 20 empty tables. No contents but the tables, every one in it its own text frame. Your [Total] and [Stories] character count will be 20.

                                           

                                          If you want to count thiese as characters in favour of your author you have to decide yourself.

                                           

                                          Same goes for "Notes" (in this case I do NOT mean "footnotes" but "notes"). Are there notes included? You have to decide if you want to count the notes special character or even the contents of notes.

                                           

                                          Or if you decide: hm, let's do not count empty paragraphs or empty lines, you have to add a lot more functionality to an otherwise simple script.

                                           

                                          @Marijan – your script is working  well. Thank you for the effort.

                                          In case there are no tables present it will brake.

                                           

                                          Maybe you could add a try/catch statement in the var section of your two table related variables like that:

                                           

                                          var
                                              myDocument = app.activeDocument,
                                              docStories = myDocument.stories.everyItem(),
                                              docCharacters = docStories.characters.length,
                                              docWords = docStories.words.length,
                                              docFootnotesCharacters = docStories.footnotes.everyItem().characters.length,
                                              docFootnotesWords = docStories.footnotes.everyItem().words.length,
                                          
                                              statReport = [];
                                                  
                                          try{
                                          
                                          var    
                                              docTablesCharacters = docStories.tables.everyItem().cells.everyItem().characters.length,
                                              docTablesWords = docStories.tables.everyItem().cells.everyItem().words.length;
                                          
                                          }catch(e){
                                               docTablesCharacters = 0;
                                               docTablesWords = 0;
                                               };
                                          

                                           

                                           

                                          Uwe

                                          • 19. Re: how to get character count
                                            Marijan Tompa [tomaxxi] Level 4

                                            @Uwe:

                                             

                                            Take a look at comment 15. I've posted simple workaround.

                                            • 20. Re: how to get character count
                                              Laubender Adobe Community Professional & MVP

                                              @Marijan – ok. Sorry, I missed that. That's working better…

                                               

                                              Uwe

                                              • 21. Re: how to get character count
                                                Laubender Adobe Community Professional & MVP

                                                @Marijan – another thing:

                                                characters of tables inside other tables will not be counted.

                                                I think you already know that. But I'd like to point this out to Anthony as well.

                                                 

                                                So maybe a different approach is better to get the total character count.

                                                 

                                                A GREP search for a single character and counting the length of the result excluding the special characters for anchored or inline objects:

                                                 

                                                app.findGrepPreferences = app.changeGrepPreferences = null;
                                                
                                                app.findChangeGrepOptions.includeMasterPages = true; //false, if you don't want to include master pages
                                                app.findChangeGrepOptions.includeFootnotes = true; //false, if you don't want to include footnotes
                                                app.findChangeGrepOptions.includeHiddenLayers = true; //false, if you don't want to include hidden layers
                                                //for find GREP only:
                                                app.findChangeGrepOptions.includeLockedLayersForFind = true; //false, if you don't want to include locked layers
                                                //for find GREP only:
                                                app.findChangeGrepOptions.includeLockedStoriesForFind = true; //false, if you don't want to include locked stories
                                                
                                                app.findGrepPreferences.findWhat = "\.";
                                                
                                                var _d = app.documents[0];
                                                
                                                //That will find too much:
                                                var _resultLength = _d.findGrep().length;
                                                
                                                app.findGrepPreferences = app.changeGrepPreferences = null;
                                                //Let's EXCLUDE special characters of anchored or inline objects:
                                                app.findGrepPreferences.findWhat = "\uFFFC";
                                                
                                                //A second search will correct that:
                                                var _resultSpecialCharacter = _d.findGrep().length;
                                                
                                                alert("Total:"+"\t"+(_resultLength - _resultSpecialCharacter)+" "+"characters");
                                                
                                                app.findGrepPreferences = app.changeGrepPreferences = null;
                                                

                                                 

                                                In that case you can easily in- or exclude texts on hidden or locked layers, in- or exclude footnotes or texts on master pages.

                                                 

                                                Uwe

                                                • 22. Re: how to get character count
                                                  Laubender Adobe Community Professional & MVP

                                                  And here we have a version that will EXCLUDE texts on the pasteboard (we can further refine it if we want to exclude special characters for text variables like "File Name" etc.pp.). The script is CS5 and CS5.5 only:

                                                   

                                                  //CountTotalCharactersOfPages.jsx
                                                  //DESCRIPTION:Excludes all texts on the pasteboard; hidden texts on pages will be counted; every instance of a used text variable will be counted as one character!
                                                  
                                                  //Uwe Laubender
                                                  
                                                  /**
                                                  * @@@BUILDINFO@@@ CountTotalCharactersOfPages.jsx !Version! Wed Jan 25 2012 16:08:46 GMT+0100
                                                  */
                                                  
                                                  //IDEA:
                                                  
                                                  //To mask text frames on the pasteboard:
                                                  //1. we have to move them to a special layer
                                                  //2. lock that layer
                                                  //3. change the search so that locked layers are excluded
                                                  
                                                  //After returning the result, go back to the old state
                                                  
                                                  app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;
                                                  
                                                  if(parseFloat(app.version)<7){
                                                      alert("Your InDesign app version should be version 7 (CS5) or 7.5 (CS5.5)");
                                                      exit();
                                                      };
                                                  
                                                  if(parseFloat(app.version)>7.6){
                                                      alert("Script is not tested for InDesign above version 7.5 (CS5.5)");
                                                      exit();
                                                      };
                                                  
                                                  app.doScript(_CountCharacters, undefined, app.selection[0], UndoModes.entireScript, "Count characters");
                                                  
                                                  //One undo will get you back to the initial state of the document:
                                                  app.undo();
                                                  
                                                  function _CountCharacters(){
                                                  
                                                  var _d = app.documents[0];
                                                  //Add a new layer; if it's already there set the variable to that layer:
                                                  try{
                                                      var _pasteBoardItemsLayer = _d.layers.add({name:"PasteBoardItems"});
                                                      }catch(e){
                                                          alert("There already is a layer named PasteBoardItems; rename it and try again!");
                                                          exit();
                                                          };
                                                  
                                                  //Unlock ALL layers in one go:
                                                  _d.layers.everyItem().locked = false;
                                                  
                                                  //Put all pasteboard items to that layer "PasteBoardItems":
                                                  for(var n=0;n<_d.pageItems.length;n++){
                                                      if(_d.pageItems[n].parentPage === null){
                                                          _d.pageItems[n].locked = false;
                                                          _d.pageItems[n].itemLayer = _pasteBoardItemsLayer;
                                                          };
                                                      };
                                                  
                                                  //Lock the _pasteBoardItemsLayer:
                                                  
                                                  _pasteBoardItemsLayer.locked = true;
                                                  
                                                  app.findGrepPreferences = app.changeGrepPreferences = null;
                                                  
                                                  app.findChangeGrepOptions.includeMasterPages = true; //false, if you don't want to include master pages
                                                  app.findChangeGrepOptions.includeFootnotes = true; //false, if you don't want to include footnotes
                                                  app.findChangeGrepOptions.includeHiddenLayers = true; //false, if you don't want to include hidden layers
                                                  //for find GREP only:
                                                  //Do NOT include locked layers in the search:
                                                  app.findChangeGrepOptions.includeLockedLayersForFind = false; //true, if you want to include locked layers
                                                  //for find GREP only:
                                                  app.findChangeGrepOptions.includeLockedStoriesForFind = true; //false, if you don't want to include locked stories
                                                  
                                                  app.findGrepPreferences.findWhat = "\.";
                                                  
                                                  //That will find too much:
                                                  var _resultLength = _d.findGrep().length;
                                                  
                                                  app.findGrepPreferences = app.changeGrepPreferences = null;
                                                  //Let's EXCLUDE special characters of anchored or inline objects:
                                                  app.findGrepPreferences.findWhat = "\uFFFC";
                                                  
                                                  //A second search will correct that:
                                                  var _resultSpecialCharacter = _d.findGrep().length;
                                                  
                                                  alert("Total:"+"\t"+(_resultLength - _resultSpecialCharacter)+" "+"characters");
                                                  
                                                  app.findGrepPreferences = app.changeGrepPreferences = null;
                                                  
                                                  };
                                                  

                                                   

                                                  "White space" characters are counted, paragraph returns or soft returns are NOT counted.

                                                   

                                                  Uwe

                                                  • 23. Re: how to get character count
                                                    Loic.Aigon Adobe Community Professional

                                                    Hi,

                                                     

                                                    As I told Anthony, GREP was probably a good way to go. However, weren't any bugs with GREP ? Not sure but might have heard a few things about this.

                                                     

                                                    Loic

                                                    • 24. Re: how to get character count
                                                      AnthonyMasure Level 1

                                                      Your script take some time for long texts (300 pages), but it rocks !

                                                      I like the possibility to exclude hidden layers, perfect for exclude some notes in marges, just have to put them on another layer, hide it then count the total.

                                                      With your settings, the script can help for several cases.

                                                       

                                                      Does it include tables ? (I think so, but I haven't some to be sure of that)

                                                       

                                                      Thanks a lot,

                                                      Anthony

                                                      • 25. Re: how to get character count
                                                        Laubender Adobe Community Professional & MVP

                                                        @Anthony – yes, it does count characters in tables.

                                                        And characters of tables sitting in tables.

                                                         

                                                        The script uses InDesign's GREP search, so this functionality is implicitly built into.


                                                        Uwe

                                                        • 26. Re: how to get character count
                                                          Laubender Adobe Community Professional & MVP

                                                          Loic wrote:

                                                          However, weren't any bugs with GREP ? Not sure but might have heard a few things about this.

                                                           

                                                          As expanding the above script I found some strange things I could not explain. When writing:

                                                           

                                                          app.findGrepPreferences.findWhat = "\.";
                                                          

                                                           

                                                          all went fine.

                                                           

                                                          Now I wanted to expand the findWhat to more characters like "carriage returns" and "forced line breaks" and tried:

                                                           

                                                          app.findGrepPreferences.findWhat = "["
                                                          
                                                              +"\." //Quite a large range, but not enough
                                                              +"\u000D" //carriage return: paragraph return, column break, frame break...
                                                              +"\u000A" //forced line break
                                                              
                                                              +"]";
                                                          

                                                           

                                                          Surprisingly this found less than the simple "\."

                                                           

                                                          Whereas the following worked fine:

                                                           

                                                          app.findGrepPreferences.findWhat = "["
                                                          
                                                              +"\uFFFC" //Special character for anchored or inline objects
                                                              +"\u0018" //Special character for new variable text object instances introduced in InDesign CS4 (???)
                                                              
                                                              +"]";
                                                          

                                                           

                                                          So I went with the solution to do individual searches and concatenating their findings later.

                                                          (All tested with InDesign CS5.5 (v7.5.5.2) on Mac OSX 10.6.8)

                                                           

                                                          Uwe