11 Replies Latest reply on Sep 13, 2016 6:23 AM by jakec88782761

    looping through paragraph styles

    jakec88782761 Level 1

      Hello,

       

      I'm trying to loop through the paragraph styles in my selection with this script:

       

      var styles = app.selection[0].textStyleRanges.everyItem().appliedParagraphStyle;

      for (var i=0;i<styles;i++) {
          styles[i].name;
          }

       

      It just returns undefined.

       

      I'd also like to be able to loop through all the applied paragraph styles in a document (to find out the names).

       

      Any suggestions would be great!

       

      Thanks

        • 1. Re: looping through paragraph styles
          Sajeev Sridharan Level 4

          Select some paragraphs and try the below code:

           

          var styles = app.selection[0].textStyleRanges.everyItem().appliedParagraphStyle;
          for (var i=0;i<styles.length;i++) {//.length missing
              $.writeln(styles[i].name);
              }
          

           

          I'd also like to be able to loop through all the applied paragraph styles in a document (to find out the names).

           

          var styles = app.activeDocument.textFrames.everyItem().textStyleRanges.everyItem().appliedParagraphStyle;
          for (i=0; i<styles.length; i++)
          {
               $.writeln(styles[i].name);
          }
          
          1 person found this helpful
          • 2. Re: looping through paragraph styles
            [Jongware] Most Valuable Participant

            You are just 'saying' the name. It's like saying '42;' in the middle of a script - what is it supposed to do? If you change that line to something that shows the name, you can see it works.

             

            You also cannot 'loop' over a plain array! You need styles.length here:

             

            var styles = app.selection[0].textStyleRanges.everyItem().appliedParagraphStyle;
            for (var i=0;i<styles.length;i++) {
                alert (styles[i].name);
                }
            

             

            Finally, beware that 'textStyleRanges' may return many items for one and the same paragraph, but its associated paragraph style will still be the same. So even if you only have selected a single paragraph, you would get its style name reported once for every italic word, every differently colored word, every custom tracked phrase ... and once for all 'regular' texts in between.

            1 person found this helpful
            • 3. Re: looping through paragraph styles
              jakec88782761 Level 1

              Thanks for your replies

              Just out of curiosity, how could I avoid getting every instance of the paragraph style? Is there another property which returns just one style when it comes across many?

              • 4. Re: looping through paragraph styles
                Sajeev Sridharan Level 4

                Could you explain more precisely.

                • 5. Re: looping through paragraph styles
                  jakec88782761 Level 1

                  Where I have used multiple instances of the "body text" styles in the selection, the script returns multiple "body styles". Is there a way to have the script only return the first instance of the style? Just so that I know it is there?

                  • 6. Re: looping through paragraph styles
                    [Jongware] Most Valuable Participant

                    That is an unnecessarily convulated way of getting paragraph styles, looping over textframes instead of stories and over text style ranges instead of directly over entire paragraphs (see my other answer).

                     

                    As for printing each style only once, there are tons of different solutions. One would be to store all style names as you encounter them, sort, then only print each duplicate; another is before storing a style name, first check if it's already in the list. Me, I prefer (ab)using an object as a "dictionary":

                     

                     

                    var styles = app.activeDocument.stories.everyItem().paragraphs.everyItem().appliedParagraphStyle;
                    var stylesUsed = {};
                    for (i=0; i<styles.length; i++) 
                    {
                        stylesUsed[styles[i].name] = true;
                    }
                    for (i in stylesUsed)
                    {
                        $.writeln(i);
                    }
                    
                    • 7. Re: looping through paragraph styles
                      Laubender Adobe Community Professional & MVP

                      Hi Jake,

                      you could store the returned names into an associative array and read out this array to a list of strings.

                       

                      Example:

                       

                      var styles = app.selection[0].textStyleRanges.everyItem().appliedParagraphStyle;
                      
                      var array = [];
                      var result = [];
                      
                      // Build an associative array.
                      // Key string is the paragraph style name.
                      // Value is a number that will count up by 1, if detected again.
                      // array["Key string"] = Value
                      
                      for (var i=0;i<styles.length;i++)
                      { 
                          // If the key cannot be found, build a new key and start the numbering with 1:
                          if(array[styles[i].name] == undefined){array[styles[i].name] = 1}
                          // otherwise add 1 to the value
                          else{array[styles[i].name] = array[styles[i].name] + 1};
                      };
                      
                      for(x in array)
                      {
                          result[result.length++] = x+"\t"+array[x];
                      }
                      
                      // Find a better method to present the result. Write it to a text file, open the text file.
                      // Or write it to an InDesign document etc.pp.
                      alert("Paragraph Style Name / Number of usages:"+"\r"+result.join("\r"));
                      

                       

                      Example result with a selected text frame in an InDesign CS6 document with my German OSX system and German InDesign:

                       

                      Alert-ExampleResult.png

                       

                      The number describes the number of text style ranges where this paragraph style is used.

                      That's just an example. Could be refined to the number of usages only counting one time per paragraph etc.pp.

                       

                      I still don't get it why one is not iterating through the paragraphs of a selection to get such a list.
                      One can only have one paragraph style per paragraph.

                       

                      Note: To get the used paragraph styles of an empty story one should address the the first insertion point of that story.

                       

                      Regards,
                      Uwe

                      • 8. Re: looping through paragraph styles
                        jakec88782761 Level 1

                        Hi Laubender,

                         

                        That looks really interesting, I'd really like to know how to write the result into an indesign document.

                         

                        Thanks for your reply

                        • 9. Re: looping through paragraph styles
                          jakec88782761 Level 1

                          Hi Jongware,

                           

                          Thanks for your reply, that is definitely a better way to do it!

                          • 10. Re: looping through paragraph styles
                            Laubender Adobe Community Professional & MVP

                            jakec88782761 wrote:

                            … That looks really interesting, I'd really like to know how to write the result into an indesign document. …

                             

                            Adding a new document, adding a new text frame, adding text to that frame is very basic stuff.

                            I'd suggest you to learn a bit more about doing ExtendScript with InDesign. And also the very basics of JavaScript.

                             

                            FWIW, as an addition to my snippet above without any extras this would be:

                             

                            var resultDoc = app.documents.add();
                            var resultFrame = resultDoc.textFrames.add({geometricBounds : [0,0,"100mm","100mm"]});
                            resultFrame.contents = result.join("\r");
                            

                             

                            If you like it more formatted or as contents of a table, all that is possible, but perhaps requires a higher level of expertise than lesson one on ExtendScript.

                            Usually in this forum we discuss scripts and will not write full scripts for special purposes as a service. Sorry, that's simply not the scope here.

                             

                            There are some good sources out there. Some books about ExtendScript with InDesign and using the ExtendScript Toolkit (ESTK), a few video trainings as well.
                            And more than enough on core JavaScript. Also inspect the ESTK's Help feature provided by Adobe and read through the documents you can download there.

                             

                            The document object model (DOM) of InDesign is documented here:

                            www.jongware.com/idjshelp.html

                            (CS6 and below, also some versions of Illustrator and FrameMaker)

                             

                            InDesign CC 2015.0:

                            InDesign ExtendScript API (11.0)

                             

                            How do I start learning InDesign Scripting?

                             

                            Regards,
                            Uwe

                            • 11. Re: looping through paragraph styles
                              jakec88782761 Level 1

                              Thanks for the advice!

                               

                              I've been learning quite a bit of JavaScript but I definitely need to learn more about the InDesign Object Model!