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

how to get character count

New Here ,
Nov 20, 2011 Nov 20, 2011

Hi All,

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

Regards,

RockSelFindValue.JPG

TOPICS
Scripting
21.2K
Translate
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

Translate
New Here ,
Jan 25, 2012 Jan 25, 2012

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

Translate
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

@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

Translate
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 ,
Feb 17, 2025 Feb 17, 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>

 

 

Translate
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
LEGEND ,
Feb 18, 2025 Feb 18, 2025
LATEST

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

 

Translate
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