7 Replies Latest reply on Feb 9, 2016 7:19 AM by Loic.Aigon

    getting the names of paragraph styles in selection

    RobertKyle Level 2

      Seems like this ought to work:

       

      app.selection[0].paragraphs.everyItem().appliedParagraphStyle.name;

       

      or

       

      app.selection[0].textStyleRanges.everyItem().appliedParagraphStyle.name;

       

      (assuming, of course, that some text is selected, which I've already checked).

       

      But both snippets return "undefined." If I don't specify the name, I get an array of paragraph styles and so can loop through to extract the names. But I just wanted to check here first to see if I'm missing something obvious.

        • 1. Re: getting the names of paragraph styles in selection
          Loic.Aigon Adobe Community Professional

          Look at the constructor and you may understand what the name property returns undefined

          app.selection[0].textStyleRanges.everyItem().appliedParagraphStyle.constructor.name 
          

          Loic

          www.ozalto.com

          • 2. Re: getting the names of paragraph styles in selection
            RobertKyle Level 2

            I get that it's an array. But consider:

             

            app.documents[0].paragraphStyles.everyItem();

             

            returns a paragraph style object so

             

            app.documents[0].paragraphStyles.everyItem().names

             

            returns an array of style names

             

            I think Adobe's selection object is weaker for this kind of implementation. We're not all writing big robotic scripts that create new documents from scratch, lay them out and send them to the printer. I write a lot of smaller toolbox style scripts that have to work with whatever the user clicked on before running the script.

            • 3. Re: getting the names of paragraph styles in selection
              Loic.Aigon Adobe Community Professional

              app.documents[0].paragraphStyles.everyItem();

               

              returns a paragraph style object so

              Not exactly. What you might need to understand here is that app.documents[0].paragraphStyles is a collection, not an array. And everyItem() will consider all the items of the collection.

               

              So

              var doc = app.activeDocument;
              var pss = doc.paragraphStyles;
              pss.everyItem().constructor.name // ParagraphStyle
              

               

              The good point is that collection are sometimes way cooler than arrays because you can tweak all the items without the need of a loop.

               

              var doc = app.activeDocument;
              var pss = doc.paragraphStyles;
              pss.everyItem().pointSize = 12; //will make all paragraph style point size to 12
              

               

              But if you try to change the name for example, you will get an error of course

              var doc = app.activeDocument;
              var pss = doc.paragraphStyles;
              pss.everyItem().name = "Let's make InDesign yelling"; 
              

               

              So when you change a property through everyItem(), this property will apply to every item in the collection.

              However, when you retrieve a common property, you actually get an array. That's really logical because you just grab some data for multiple objects.

               

              In case you do need true arrays of objects, you can use "getElements()" will which throw all the collections items in a form of an array:

               

              var doc = app.activeDocument;
              var pss = doc.paragraphStyles;
              pss.everyItem().getElements().constructor.name; //Array
              

               

               

              HTH,

              Loic

              www.ozalto.com

              • 5. Re: getting the names of paragraph styles in selection
                Obi-wan Kenobi Adobe Community Professional

                Hi Robert, Loïc and Peter,

                 

                Only for comment, a very cool script written by Kasyan Servestky:

                 

                http://kasyan.ho.com.ua/export_styles_description_settings_to_csv.html

                 

                 

                • 6. Re: getting the names of paragraph styles in selection
                  RobertKyle Level 2

                  OK, so now I understand that "app.selection[0].paragraphs.everyItem()" is an array, and I was expecting it to be a collection. I'll freely admit that I did not understand the difference between the two. Now that I have been educated on that point, I can't help but wonder why. Seems that app.selection would be more useful if it were a collection.

                   

                  But the main point is: To get the names of the paragraph styles in the currently selected type, there is no alternative to a loop.

                  • 7. Re: getting the names of paragraph styles in selection
                    Loic.Aigon Adobe Community Professional

                    Hi,

                    I may have misexplained myself.

                    " I understand that "app.selection[0].paragraphs.everyItem()" is an array, and I was expecting it to be a collection."

                    No it's the opposite. app.selection is be an array (be aware that  selection returns null if no documents open). But since you adress paragragraphs objects, you get a collection.


                    "Seems that app.selection would be more useful if it were a collection."

                    Well think of a selection made of some text frames, some rectangles and some multimedia stuff, what would be the collection type here ? Collection only apply when you retrieve objects of a same type through something like app.documents…