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 – ok. Sorry, I missed that. That's working better…

Uwe


@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