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

Need help to count characters in multiple Text Layers

Community Beginner ,
Jul 08, 2015 Jul 08, 2015

Copy link to clipboard

Copied

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();

TOPICS
Actions and scripting

Views

731

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

Community Expert , Jul 16, 2015 Jul 16, 2015

// 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

...

Votes

Translate

Translate
Adobe
Community Expert ,
Jul 08, 2015 Jul 08, 2015

Copy link to clipboard

Copied

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

};

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 Beginner ,
Jul 08, 2015 Jul 08, 2015

Copy link to clipboard

Copied

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

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 ,
Jul 08, 2015 Jul 08, 2015

Copy link to clipboard

Copied

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.

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 ,
Jul 09, 2015 Jul 09, 2015

Copy link to clipboard

Copied

Actually you could just join the Array to make it a String and then perform the existing operation on it.

I see problems with the existing code, though, in that a space at the end of the String and double-spaces in the text affect the word count.

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 Beginner ,
Jul 16, 2015 Jul 16, 2015

Copy link to clipboard

Copied

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

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 ,
Jul 16, 2015 Jul 16, 2015

Copy link to clipboard

Copied

// 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");

};

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 Beginner ,
Jul 18, 2015 Jul 18, 2015

Copy link to clipboard

Copied

AWESOME WORKS LIKE A CHARM!! THANKS A LOT!!

I have a small query on this. is there any way that it counts the characters on the active text Layers. Now it's actually counts the whole document and if possible can you add a alert when no text layers are selected? I Mean  some thing like this

else

    {

    alert("Select text layers");

    }

Thanks in advance

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 ,
Jul 19, 2015 Jul 19, 2015

Copy link to clipboard

Copied

You could use this

////// by paul mr;

function getSelectedLayersIdx(){

      var selectedLayers = new Array;

      var ref = new ActionReference();

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

      var desc = executeActionGet(ref);

      if( desc.hasKey( stringIDToTypeID( 'targetLayers' ) ) ){

         desc = desc.getList( stringIDToTypeID( 'targetLayers' ));

          var c = desc.count

          var selectedLayers = new Array();

          for(var i=0;i<c;i++){

            try{

               activeDocument.backgroundLayer;

               selectedLayers.push(  desc.getReference( i ).getIndex() );

            }catch(e){

               selectedLayers.push(  desc.getReference( i ).getIndex()+1 );

            }

          }

       }else{

         var ref = new ActionReference();

         ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "ItmI" ));

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

         try{

            activeDocument.backgroundLayer;

            selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" ))-1);

         }catch(e){

            selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" )));

         }

      }

      return selectedLayers;

};

to get the indices of the selected Layers and adjust the for-function that collects the texts accordingly.

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 Beginner ,
Jul 19, 2015 Jul 19, 2015

Copy link to clipboard

Copied

Thanks for this..but I'm getting a error in some line(var theTexts = main (); ).. Can you please help in fixing the whole??

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 ,
Jul 19, 2015 Jul 19, 2015

Copy link to clipboard

Copied

LATEST
Can you please help in fixing the whole??

Please post the code.

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