4 Replies Latest reply on Apr 2, 2014 11:26 AM by Jump_Over

    Word count and then Pasting text in to text block

    davidnest Level 1

      I'm trying to create a javascript that counts the words in a selected text block, adds " WORDS." to the end  and then pastes it at the beginning of that text block with a Bold character style applied. So it would end up looking like:

       

      25 WORDS. Text text text text text. etc.

       

      I've managed to figure out how to create the character style, count the words in the selected text block and give me an alert. But I'm not sure how to apply the character style and paste it at the beginning. Any help would be really appriciated. Here is what I have so far.

       

      Thanks

      -----------------------

       

      var curDoc = app.activeDocument;

      var mySel = app.selection[0];

       

       

      //Create wordcount style

       

      if (!curDoc.characterStyles.item("wordcount").isValid) {

          var cStyle = curDoc.characterStyles.add({name: "wordcount"});

      }

      else {

          var cStyle = curDoc.characterStyles.item("wordcount");

      }

       

      cStyle.fontStyle = "Bold";

      //

       

       

      var mySelWords = mySel.texts.everyItem().words.length;

      var mySelStoryWords = mySel.parentStory.words.length;

       

      alert( mySelWords + " WORDS.");

        • 1. Re: Word count and then Pasting text in to text block
          Jump_Over Level 5

          Hi,

           

          All you need is to apply a proper cStyle to 1st insertionPoint of mySel and fill it with created string,

          i.e.:

           

          var 
              curDoc = app.activeDocument,
              mySel = app.selection[0],
              cStyle = curDoc.characterStyles.item("wordcount"),
              mySel = app.selection[0],
              mySelWords = mySel.texts.everyItem().words.length,
              mString = mySelWords + " Words. ";
          
          if (!cStyle.isValid) 
              cStyle = curDoc.characterStyles.add({name: "wordcount", fontStyle: "Bold"});
          
          mySel.insertionPoints[0].appliedCharacterStyle = cStyle;
          mySel.insertionPoints[0].contents = mString;
          

           

          Jarek

          1 person found this helpful
          • 2. Re: Word count and then Pasting text in to text block
            davidnest Level 1

            So great, thanks for the help. I'm just learning this and it's very interesting how you declared all those var at once.

            Thanks again!

            • 3. Re: Word count and then Pasting text in to text block
              davidnest Level 1

              To push this even further. Would it be possible to add some type of Until statement. So I could select multiple text frames, add a word count to the first and then move on the next frame.

              • 4. Re: Word count and then Pasting text in to text block
                Jump_Over Level 5

                Hi,

                 

                going further, through the selection, modify this alike:

                var 
                     curDoc = app.activeDocument,
                     mySel = app.selection,
                     len = mySel.length,
                     cStyle = curDoc.characterStyles.item("wordcount"),
                     mySelWords, mString, curSel;
                
                if (!cStyle.isValid) 
                    cStyle = curDoc.characterStyles.add({name: "wordcount", fontStyle: "Bold"});
                
                     while (len-->0) {
                          curSel = mySel[len];
                          if (curSel.hasOwnProperty ("insertionPoints") ) {
                               mySelWords = curSel.texts.everyItem().words.length,
                               mString = mySelWords + " Words. ";
                               curSel.insertionPoints[0].appliedCharacterStyle = cStyle;
                               curSel.insertionPoints[0].contents = mString;
                               }
                          }
                

                 

                Notice that textFrame.words count is the ones visible only, so overflown part of text is excluded.

                 

                If some answers were helpful - mark it (them ), pls ==> this is a good flag for other searchers..., and some points for me.., mlask.

                 

                Jarek