Skip to main content
Participant
November 21, 2011
Answered

how to get character count

  • November 21, 2011
  • 2 replies
  • 22151 views

Hi All,

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

Regards,

RockSel

Correct answer csm_phil

Ok Thank you so much.

How to select whole document with out master pages.


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

2 replies

thkr_techcollege
Participant
February 18, 2025

AI generated Javascript that counts only visible text elements. It counts both characters and empty space, but not line breaks. Put the needed elements into one layer and hide the others, before making a count. Hope its relevant

 

<code>
(function ()

{ if (app.documents.length === 0) {

alert("No open documents");

return;

}

var doc = app.activeDocument;

var visibleText = "";



// Check all layers are visible

for (var i = 0; i < doc.layers.length; i++){

var layer = doc.layers[i];



if (layer.visible) { // Only visible layers

var textFrames = layer.textFrames;



for (var j = 0; j < textFrames.length; j++) {

visibleText += textFrames[j].contents; // Only add tet from visible layers



}

}

}



var cleanedText = visibleText.replace(/[\r\n]/g, ""); // Remove line break

var charCount = cleanedText.length; // Characters incl. word space



alert("Number of characters (incl. word space, ex. line break, only visible layers): " + charCount); })();
</code>

 

 

Robert at ID-Tasker
Legend
February 18, 2025

@thkr_techcollege

 

Not really.

 

You don't have to put "needed elements into one layer and hide the others" - as this script will count elements from all visible layers anyway.

 

This comment is misleading: 

// Check all layers are visible

It's not what this loop is doing.

 

And overall - it's not optimal - instead of building bigger and bigger string - it could count number of characters for each element and increase total counter. 

 

Also, it doesn't count texts in Tables. 

 

And the thread is 13 years old... 

 

csm_phil
Legend
November 21, 2011

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

கற_பன___Imagine_
Inspiring
November 21, 2011

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.

Community Expert
January 25, 2012

@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


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