• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

how to get character count

New Here ,
Nov 20, 2011 Nov 20, 2011

Copy link to clipboard

Copied

Hi All,

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

Regards,

RockSelFindValue.JPG

TOPICS
Scripting

Views

18.2K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Advocate , Nov 20, 2011 Nov 20, 2011

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

Votes

Translate

Translate
Advocate ,
Nov 20, 2011 Nov 20, 2011

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Nov 20, 2011 Nov 20, 2011

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 20, 2011 Nov 20, 2011

Copy link to clipboard

Copied

Ok Thank you so much.

How to select whole document with out master pages.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Nov 20, 2011 Nov 20, 2011

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 21, 2011 Nov 21, 2011

Copy link to clipboard

Copied

@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.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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 24, 2012 Jan 24, 2012

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Jan 24, 2012 Jan 24, 2012

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 24, 2012 Jan 24, 2012

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Jan 24, 2012 Jan 24, 2012

Copy link to clipboard

Copied

Also check out this one:

http://tomaxxi.com/2010/12/quicktip-counting-text-using-everyitem/

--

Marijan (tomaxxi)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 24, 2012 Jan 24, 2012

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Jan 24, 2012 Jan 24, 2012

Copy link to clipboard

Copied

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)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 24, 2012 Jan 24, 2012

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Jan 24, 2012 Jan 24, 2012

Copy link to clipboard

Copied

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

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 24, 2012 Jan 24, 2012

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Jan 24, 2012 Jan 24, 2012

Copy link to clipboard

Copied

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)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 24, 2012 Jan 24, 2012

Copy link to clipboard

Copied

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)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Jan 25, 2012 Jan 25, 2012

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 25, 2012 Jan 25, 2012

Copy link to clipboard

Copied

@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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Jan 25, 2012 Jan 25, 2012

Copy link to clipboard

Copied

@Uwe:

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 25, 2012 Jan 25, 2012

Copy link to clipboard

Copied

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

Uwe

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 25, 2012 Jan 25, 2012

Copy link to clipboard

Copied

@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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 25, 2012 Jan 25, 2012

Copy link to clipboard

Copied

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.parentPage === null){

        _d.pageItems.locked = false;

        _d.pageItems.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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Jan 25, 2012 Jan 25, 2012

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 27, 2012 Jan 27, 2012

Copy link to clipboard

Copied

LATEST

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines