Skip to main content
Participant
July 8, 2015
Answered

Need help to count characters in multiple Text Layers

  • July 8, 2015
  • 1 reply
  • 1242 views

I'm using below code for counting number of characters and words in a single text layer. Is that possible to count number of characters and words in a multiple text layers.??Please Help

Java Script Code:

// 

/*

<javascriptresource>

<name>Count Words and Characters</name>

</javascriptresource>

*/

function run()

{

var layer = activeDocument.activeLayer;

    if (layer.kind == LayerKind.TEXT)

        {

  

    var words = layer.textItem.contents;

            words = words.replace(/(\r\n|\n|\r)/gm," ");

var countwords = words.split(" ").length;

var countletters = words.split(" ").join(" ").length;

alert("\n " + countwords + " Words\n " + countletters + " Characters","Count");

}

else

    {

    alert("Select a text layer.","Count");

    }

}

run();

This topic has been closed for replies.
Correct answer c.pfaffenbichler

Thanks for that! But it is showing error when I make it as a string.can you please help me with the whole code?


// 2015, use it at your own risk; 

#target "photoshop-70.032"

if (app.documents.length > 0) { 

var theTexts = main (); 

alert (count (theTexts.join("\n")));

}; 

//////////////////////////////////// 

function main () { 

// the file; 

var myDocument = app.activeDocument; 

// get number of layers; 

var ref = new ActionReference(); 

ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 

var applicationDesc = executeActionGet(ref); 

var theNumber = applicationDesc.getInteger(stringIDToTypeID("numberOfLayers")); 

// process the layers; 

var theLayers = new Array; 

for (var m = 0; m <= theNumber; m++) { 

try { 

var ref = new ActionReference(); 

ref.putIndex( charIDToTypeID( "Lyr " ), m); 

var layerDesc = executeActionGet(ref); 

var layerSet = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("layerSection"))); 

var isBackground = layerDesc.getBoolean(stringIDToTypeID("background")); 

// if not layer group ; 

if (layerSet != "layerSectionEnd" && layerSet != "layerSectionStart" && isBackground != true) { 

var theName = layerDesc.getString(stringIDToTypeID('name')); 

var theID = layerDesc.getInteger(stringIDToTypeID('layerID')); 

var hasText = layerDesc.hasKey(stringIDToTypeID("textKey")); 

// if type layer; 

if (hasText == true) { 

var textDesc = layerDesc.getObjectValue(stringIDToTypeID('textKey')); 

var theText = textDesc.getString(stringIDToTypeID("textKey")); 

theLayers.push(theText) 

catch (e) {}; 

}; 

return theLayers

};

////// count characters //////

function count (theString) { 

var words = theString//layer.textItem.contents;

words = words.replace(/(\r\n|\n|\r)/gm," ");

////////////////////////////////////

// remove double spaces and space at the end;

while (words.indexOf("  ") != -1) {words = words.replace("  ", " ")};

var endSpace = /\s$/;

words = words.replace(endSpace, "");

////////////////////////////////////

var countwords = words.split(" ").length;

var countletters = words.split(" ").join(" ").length;

return ("\n " + countwords + " Words\n " + countletters + " Characters");

};

1 reply

c.pfaffenbichler
Community Expert
Community Expert
July 8, 2015

This should get you an Array of the Type Layers’ texts which you could then process.

// 2015, use it at your own risk;

#target photoshop

if (app.documents.length > 0) {

var theTexts = main ();

alert (theTexts.join("\n"));

};

////////////////////////////////////

function main () {

// the file;

var myDocument = app.activeDocument;

// get number of layers;

var ref = new ActionReference();

ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

var applicationDesc = executeActionGet(ref);

var theNumber = applicationDesc.getInteger(stringIDToTypeID("numberOfLayers"));

// process the layers;

var theLayers = new Array;

for (var m = 0; m <= theNumber; m++) {

try {

var ref = new ActionReference();

ref.putIndex( charIDToTypeID( "Lyr " ), m);

var layerDesc = executeActionGet(ref);

var layerSet = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("layerSection")));

var isBackground = layerDesc.getBoolean(stringIDToTypeID("background"));

// if not layer group ;

if (layerSet != "layerSectionEnd" && layerSet != "layerSectionStart" && isBackground != true) {

var theName = layerDesc.getString(stringIDToTypeID('name'));

var theID = layerDesc.getInteger(stringIDToTypeID('layerID'));

var hasText = layerDesc.hasKey(stringIDToTypeID("textKey"));

// if type layer;

if (hasText == true) {

var textDesc = layerDesc.getObjectValue(stringIDToTypeID('textKey'));

var theText = textDesc.getString(stringIDToTypeID("textKey"));

theLayers.push(theText)

}

}

}

catch (e) {};

};

return theLayers

};

Participant
July 8, 2015

Thanks a lot for this..

But i don't need the text to be displayed in the window. I need only the count which shows number of characters and words on the active layers. My script only shows for one active layer. How can I count for multiple active text layers??

Thanks in advance

c.pfaffenbichler
Community Expert
Community Expert
July 8, 2015

You don’t need the alert so remove it and perform what you so far performed on the text of one Layer on the texts collected in the Array with a for-clause.