17 Replies Latest reply: Dec 7, 2011 6:51 AM by BenCloutier RSS

    Creating a Style List

    Eugene Tyson MVP

      I'm looking for a script that could list out the name of my styles and apply that style to the text in the list?

       

      Something like this

       

      Untitled-1.jpg

        • 1. Re: Creating a Style List
          [Ariel] Community Member

          Which style do you want to apply to the text in the list? (Using email

          and can't see the picture)

           

          Ariel

          • 2. Re: Creating a Style List
            [Ariel] Community Member

            Also, is it a list of paragraph styles that you want?

            Plus, how do you want the script to deal with style groups?

             

            Ariel

            • 3. Re: Creating a Style List
              Eugene Tyson MVP

              Just to list them out in the order they are in. From top to bottom.

               

              Basically, the text should read the Style name, then apply that style to the text.

               

              Heading 1

              Heading 2

              Heading 3

              Body Text

              Body Text Indent

               

               

              and so on

              • 4. Re: Creating a Style List
                [Ariel] Community Member

                I'm ignoring groups, and presuming we're on CS4 (and possibly upwards)

                and that you've got your text cursor in an empty text frame, and I

                haven't tested this much because I'm right in the middle of something,

                but here goes:

                 

                myDoc = app.activeDocument;

                myStyles = myDoc.allParagraphStyles;

                myStory = app.selection[0].parentStory;

                for (a=0; a<myStyles.length; a++){

                   myStory.insertionPoints[-1].contents = myStyles[a].name+"\r";

                   myStory.paragraphs[-1].appliedParagraphStyle = myStyles[a];

                }

                 

                Does that help?

                 

                Ariel

                • 5. Re: Creating a Style List
                  Philippe Ruelle Community Member

                  List of style carataires.
                  List of paragraph style and its réapliqué

                  For me it works, the code needs to be improved ...


                  Attention to the character in the names of styles (no (), ° §) it poses problems

                   

                  Translation of French


                   

                  Liste des style de carataires.

                  Liste des style de paragraphes et il son réapliqué

                   

                  Pour moi ça marche, le code demande à être amélioré ...

                  Attention au caractère dans le nom des styles ( pas de (), °, §) sa pose des problème

                   

                   

                   

                  var leNombreDeGroupeCara = app.activeDocument.characterStyleGroups.length; // conteur de groupr de style  para

                  var lesStyleDocCara = [];

                  lesStyleDocCara = styleDocCar();

                  if (leNombreDeGroupeCara != 0 ) {

                  for (i = 0; i < leNombreDeGroupeCara; i++) { // for 01

                         var conteurStyle = app.activeDocument.characterStyleGroups[i].characterStyles.length;           

                              for (y = 0; y < conteurStyle; y++) { // for 02

                                 lesStyleDocCara.push(app.activeDocument.characterStyleGroups[i].characterStyles[y].name);

                              } // for 02

                          } // fin du FOR 01      

                      } else {

                          alert(leNombreDeGroupeCara);

                    } // fin du IF

                  var leNombreDeGroupePara = app.activeDocument.paragraphStyleGroups.length; // conteur de groupr de style  para

                  var lesStyleDoc = [];

                  lesStyleDoc = styleDocPara();

                  if (leNombreDeGroupePara != 0 ) { 

                      for (i = 0; i < leNombreDeGroupePara; i++) { // for 01

                          var conteurStyle = app.activeDocument.paragraphStyleGroups[i].paragraphStyles.length;       

                              for (y = 0; y < conteurStyle; y++) { // for 02

                                 lesStyleDoc.push(app.activeDocument.paragraphStyleGroups[i].paragraphStyles[y].name);

                              } // for 02

                          } // fin du FOR 01

                      } else {

                          alert(leNombreDeGroupePara);

                          //$.writeln (leNombreDeGroupePara);

                  } // fin du IF

                  myDoc = app.activeDocument;

                  myStory = app.selection[0].parentStory;

                  myStory.insertionPoints[-1].contents = "Style Cara\r";

                  for (a=0; a<lesStyleDocCara.length; a++){

                     myStory.insertionPoints[-1].contents = lesStyleDocCara[a]+"\r";

                  }

                  myStory.insertionPoints[-1].contents = "Style Para\r";

                  for (a=1; a<lesStyleDoc.length; a++){

                     myStory.insertionPoints[-1].contents = lesStyleDoc[a]+"\r";

                     if (app.activeDocument.paragraphStyles.item(lesStyleDoc[a])){

                         try{

                          myStory.paragraphs[-1].appliedParagraphStyle = app.activeDocument.paragraphStyles.item(lesStyleDoc[a]);

                          }

                          catch (e){

                              var leStyle = lesStyleDoc[a];

                              myStory.paragraphs[-1].appliedParagraphStyle = app.activeDocument.paragraphStyleGroups[0].paragraphStyles.item(leStyle);

                          }

                      }

                    } //

                  function styleDocCar() {

                       var variableLocal = app.activeDocument.characterStyles.everyItem().name.join("|");

                       return variableLocal.split("|");

                  }// fin de 

                  function styleDocPara() {

                       var variableLocal = app.activeDocument.paragraphStyles.everyItem().name.join("|");

                       return variableLocal.split("|");

                  }// fin de

                  • 6. Re: Creating a Style List
                    Philippe Ruelle Community Member

                    Hi

                     

                    I have treated one group of style, there should be a routine test in all the group!

                     

                    Translation Frabçais

                     

                    je n'ai traité qu'un seul groupe de style, il faudrais faire une routine qui test dans tous les groupe!

                     

                    Philou

                    • 7. Re: Creating a Style List
                      [Jongware] MVP

                      Short & sweet. Wasn't there a post on InDesignSecrets, by the way, doing the same? (Can't be bothered to look it up because this is way more fun.)

                       

                       

                      if (app.documents.length > 0 && app.selection.length == 1 && app.selection[0].hasOwnProperty("baseline"))
                                for (p=2; p<app.activeDocument.allParagraphStyles.length; p++)
                                {
                                          name = app.activeDocument.allParagraphStyles[p].name;
                                          par = app.activeDocument.allParagraphStyles[p].parent;
                                          while (par instanceof ParagraphStyleGroup)
                                                    name = par.name+" : "+name, par = par.parent;
                                          app.selection[0].parentStory.insertionPoints[-1].properties = {contents:name+"\r", appliedParagraphStyle:app.activeDocument.allParagraphStyles[p]};
                                }
                      
                      • 8. Re: Creating a Style List
                        Eugene Tyson MVP

                        Thanks everyone. I liked Jongware's version it was a lot shorter and did exactly what I wanted.

                         

                        Thanks everyone

                        • 9. Re: Creating a Style List
                          Maria964 Community Member

                          I Jongware,

                           

                          This one is a very useful script for me and I supose for a lot of people. But it whould be better if it executes only with the paragrah styles that are inside a Style Group. Can you do this for me?

                           

                          Thank you in advance

                           

                          Maria

                          • 10. Re: Creating a Style List
                            [Jongware] MVP

                            Yeah sure.

                             

                            if (app.documents.length > 0 && app.selection.length == 1 && app.selection[0].hasOwnProperty("baseline"))
                             for (p=2; p<app.activeDocument.allParagraphStyles.length; p++)
                             {
                              if (app.activeDocument.allParagraphStyles[p].parent instanceof ParagraphStyleGroup)
                              {
                               name = app.activeDocument.allParagraphStyles[p].name;
                               par = app.activeDocument.allParagraphStyles[p].parent;
                               while (par instanceof ParagraphStyleGroup)
                                name = par.name+" : "+name, par = par.parent;
                               app.selection[0].parentStory.insertionPoints[-1].properties = {contents:name+"\r", appliedParagraphStyle:app.activeDocument.allParagraphStyles[p]};
                              }
                             }
                            
                            • 11. Re: Creating a Style List
                              Laubender MVP

                              @Jongware:
                              Hm. Maybe it's better to set p=1 so we can catch the [Basic Paragraph] style.

                               

                              Uwe

                              • 12. Re: Creating a Style List
                                [Jongware] MVP

                                I intentionally skipped it because your documents can catch some contagious stuff from it!

                                 

                                (Interested lurchers: I started with index #2 because

                                 

                                #0 is [No Paragraph Style]

                                #1 is [Basic Paragraph Style]

                                 

                                Including [No Paragraph Style] is useless, as it's always equal to [Basic]. [Basic] can be redefined and used; however, I think in general it's advised not to, because copying text with a Basic style or one based on it to a document with a different definition Will Mess You Up.)

                                • 13. Re: Creating a Style List
                                  Eugene Tyson MVP

                                  I'm glad you skipped the Basic Paragraph Style - it's not needed at all. It shouldn't even be in the program from the start.

                                  • 14. Re: Creating a Style List
                                    absqua Community Member

                                    Hear, hear!

                                    • 15. Re: Creating a Style List
                                      Maria964 Community Member

                                      Thank you very much

                                       

                                      Maria

                                      • 16. Re: Creating a Style List
                                        Philippe Ruelle Community Member

                                        Congratulations class! [Jongware]

                                        • 17. Re: Creating a Style List
                                          BenCloutier Community Member

                                          Wow, always impressed! I added the Character style based on that script (changed all "Paragraph" to "Character" and  "p=2" to "p=1")