5 Replies Latest reply on Oct 13, 2011 2:49 PM by winterm

    Help: apply different para styles to several consecutive paragraphs via script

    winterm Level 4

      Hi all,

      I need to apply different para styles to several consecutive paragraphs via script, something like this (found somewhere on the net):

       

      var myDoc = app.documents[0];

      var mySel = app.selection[0];

       

      var myPStyle1 = "A";

      var myPStyle2 = "B";

      //need to add 6 more styles here:

      //var myPStyle3 = "C";

      //var myPStyle4 = "D";

      //var myPStyle5 = "E";

      //var myPStyle6 = "F";

      //var myPStyle7 = "G";

      //var myPStyle8 = "H";

      //the last style not needed in script?

       

      // apply 1st style to 1st paragraph

      mySel.appliedParagraphStyle = myDoc.paragraphStyles.item (myPStyle1);

       

      // apply styles to paragraphs after selected

      //2nd style

      mySel.paragraphs[-1].insertionPoints[-1].appliedParagraphStyle = myDoc.paragraphStyles.item (myPStyle2);

       

      //next style

      mySel.paragraphs[-1].insertionPoints[-1].paragraphs[0].insertionPoints[-1].appliedParagrap hStyle = myDoc.paragraphStyles.item (myPStyle2).nextStyle;

       

      I've tried badly, but can't find the way to extend the sequence

      Sorry, no skills in scripting yet...

       

      all styles have 'Next Style' option activated, but I can't use 'Apply style, then Next style' or Object styles, because I need four different sequences of those para styles:

      A-B-C-D-E-F-G-H

      A-B-D-E-G-H

      A-C-D-F-G-H

      A-D-G-H

      and I really don't want to create special clones of the same style for each sequence (Keep It Simple, they say ).

       

      I believe it can be done via four slightly adjusted scripts...

      any suggestions would be greatly appreciated...

        • 1. Re: Help: apply different para styles to several consecutive paragraphs via script
          winterm Level 4

          oh, I knew it's basic, but I didn't even guess it's sooo basic...

          thank you for not answering guys, I found out it...

          happy scripting

          • 2. Re: Help: apply different para styles to several consecutive paragraphs via script
            Brian.T.Tanner

            Please post your solution so that others can benefit from it!

            • 3. Re: Help: apply different para styles to several consecutive paragraphs via script
              winterm Level 4

              oh well, here it is...

               

              var mySel = app.selection[0];
              var myStory = mySel.parentStory;
              
              var myPStyle1 = "paragraph 1";
              var myPStyle2 = "paragraph 2";
              var myPStyle3 = "paragraph 3";
              //you can add as much styles as you need here...
              
              myStory.paragraphs[0].appliedParagraphStyle = myPStyle1;
              myStory.paragraphs[1].appliedParagraphStyle = myPStyle2;
              myStory.paragraphs[2].appliedParagraphStyle = myPStyle3;
              //...and here, just specify sequence
              

               

              it's simple, primitive, and not flexible. Has a lot of limitations: works on separate story only (not text frame!), formats given number of paragraphs from the beginning of the story, no matter where you click (I didn't hassle with insertionPoints). It ignores 'next Style' option in paragraph style - it can be set, can be not - no difference.

              But it fits my specific needs in project I'm currently working on...

               

              If someone of newbies (like me) has the time and eager to experiment and add some useful 'features' here - you're highly welcome

              • 4. Re: Help: apply different para styles to several consecutive paragraphs via script
                [Jongware] Most Valuable Participant

                Sorry about being late to the party, I usually pick up stuff like this right away.

                How about this one?

                 

                1. No separate scripts needed, it uses a tiny dialog where you can choose what style set to use. If I'm correct, you can press a number key on Windows to immediately select one of the items.

                2. It works down from the paragraph your cursor is in. It has nothing to do with text frames, though -- consecutive paragraphs inside a story always ignore any text frames.

                3. Extensible: you can easily change the names of the styles in the top array, and the number of style groups in the list.

                 

                var styleLists = [
                          [ "A", "B", "C", "D", "E", "F", "G", "H" ],
                          [ "A", "B", "D", "E", "G", "H" ],
                          [ "A", "C", "D", "F", "G", "H" ],
                          [ "A", "D", "G", "H" ]
                ];
                
                
                var radiobutts = [];
                
                
                styleDialog = app.dialogs.add ({name:"Set Multiple Styles",canCancel:true});
                with (styleDialog)
                {
                          with (dialogColumns.add())
                          {
                                    with (radiobuttonGroups.add())
                                    {
                                              for (i=0; i<styleLists.length; i++)
                                              {
                                                        // Build radio button string
                                                        str = "&"+String (i+1)+". ";
                                                        for (j=0; j<styleLists[i].length; j++)
                                                        {
                                                                  if (j) str += "-";
                                                                  str += styleLists[i][j];
                                                        }
                                                        radiobutts.push (radiobuttonControls.add({staticLabel:str, checkedState:false}) );
                                              }
                                    }
                          }
                }
                radiobutts[0].checkedState = true;
                if (styleDialog.show())
                {
                          for (i=0; i<radiobutts.length; i++)
                          {
                                    if (radiobutts[i].checkedState == true)
                                              break;
                          }
                          if (i < radiobutts.length)
                          {
                                    par = app.selection[0].paragraphs[0];
                                    for (j=0; j<styleLists[i].length; j++)
                                    {
                                              par.appliedParagraphStyle = styleLists[i][j];
                                              par = par.parentStory.paragraphs.nextItem(par);
                                    }
                          }
                }
                
                • 5. Re: Help: apply different para styles to several consecutive paragraphs via script
                  winterm Level 4

                  Wow, hats off, here came the Master! Thank You for your precious time, Jongware. Didn't test Your solution yet (burning project), but I do believe it's 'final', as always. I'll study it on weekend... it must be very interesting.