8 Replies Latest reply on Jul 6, 2018 6:45 AM by Laubender

    why count wrong

    hamdifem Level 1

      when I select a grouped object

      I want to count kvc paragraph styles in text frame

      but the result is wrong why

       

      This code;

      var myDoc = app.activeDocument;

      var selection = app.activeDocument.selection;

      var myObj = [];

           for ( j = 0; j<selection.length; ++j) {

                 if(selection[j] instanceof Group){

                      for(var g = 0; g < selection[j].allPageItems.length; ++g) {

                                var item = selection[j].allPageItems[g];

                                     if(item instanceof TextFrame) {

                                          for(var p = 0; p < item.paragraphs.length; ++p) {

                                                      if(item.paragraphs[p].appliedParagraphStyle == "kvc") {

                                                       myObj.push(item)

       

                                 }

                              }

                          }

                   }

              }

        }

      alert(myObj.length)

        • 1. Re: why count wrong
          yadavs92328 Level 2

          You forgot to match name there

          if(item.paragraphs[p].appliedParagraphStyle.name.toString() == "kvc") {
          

           

          Best

          Sunil

          • 2. Re: why count wrong
            Laubender Adobe Community Professional & MVP

            Hi,

            what's the result of your "counting"?

            What is the number of paragraphs you'd expect?

             

            Note:

            If you are looking for an applied paragraph style always compare a paragraph style and not a string.

            That's more save, even if from time to time your method is working.

             

            Declare the paragraph style before you do your compare at the start of the script and check if it is valid.

             

            var paraStyle = myDoc.paragraphStyles.itemByName("kvc");
            if( !paraStyle.isValid ){ alert("Paragraph Style not found."); exit() }; // instead of exit() use return when running this inside a funtion!
            

             

            Later in the script you could refer to variable paraStyle like this:

            if( item.paragraphs[p].appliedParagraphStyle == paraStyle )
            {
            myObj.push(item);
            break;
            }
            

             

            Important Note:

            You need break to end the inner loop at the moment the first paragraph is found.

            Otherwise you'd push your frame more than one time to the array in case a second or a third paragraph is in the frame with the "kvc" applied.

             

            Perhaps you meant that with "wrong count"?

             

             

            Regards,
            Uwe

            1 person found this helpful
            • 3. Re: why count wrong
              hamdifem Level 1

              4 text frames grouped

              within these 3 text frames

              have "kvc" paragraph style applied word

               

              alert(item.paragraphs[p].contents)

              break;

               

              3 alert ok

              but

              there are 2 of them

              one of them is empty

              • 4. Re: why count wrong
                Laubender Adobe Community Professional & MVP

                I can only guess, because you did not state if you want to add a text frame to your array that is empty and the first insertion point is formatted with paragraph style "kvc".

                 

                You should prepare for a case where no paragraphs are in a text frame.

                Or where all text is overset.

                 

                Show a sample document, best upload one to a service like Dropbox and give us the link.

                 

                Regards,
                Uwe

                • 5. Re: why count wrong
                  hamdifem Level 1

                  this link idml and script

                   

                  WeTransfer

                  • 6. Re: why count wrong
                    Laubender Adobe Community Professional & MVP

                    Do you want to add a text frame to your array that is empty and the first insertion point is formatted with paragraph style "kvc"?

                     

                    Regards,
                    Uwe

                    • 7. Re: why count wrong
                      hamdifem Level 1

                      I unsuccessful

                      if you edit code I'm glad

                      • 8. Re: why count wrong
                        Laubender Adobe Community Professional & MVP

                        Hi hamdifem ,

                        ok, let's be cautious. Instead of break use continue in the loop.

                         

                        If that will not work or will work one time and not another time try to resolve all the paragraphs of a particular text frame by using getElements() with everyItem():

                         

                        var currentParagraphs = item.paragraphs.everyItem().getElements();
                        

                         

                        and loop the array currentParagraphs where you test for the applied paragraph style.

                         

                        If a text frame is empty that will enforce an error then.

                        So first test for item.paragraphs.length in the outer loop.

                        If length is 0, loop on with continue to the next text frame before doing everyItem().getElements().

                         

                        Regards,
                        Uwe