8 Replies Latest reply on May 30, 2017 3:58 AM by AhmetSamsa

    Apply paragraph style with next style

    Roy McCoy Level 1

      Hi. I haven't been in this forum for a long time, mostly because I've wanted to concentrate on AppleScript and JavaScript seems so preponderant here. Before stating my current question I'll say that some interesting AS discussions have been happening on the Lasso InDesign list, to which I especially invite any AppleScripters: http://www.listsearch.com/InDesign/. I'm tagging my scripting messages there with "AS:" or "AS/JS:" so that people who aren't interested in scripting can filter them out, and I suppose people from here might want to filter out every message that *wasn't* tagged in this way.

       

      Apparently nobody on the Lasso list can help me with this question. Michael Brady has taught those of us there who weren't aware of it that with a multiparagraph selection you can control/right-click on a paragraph style, choose Apply "[name of style]" with Next Style, and then next styles are applied throughout the selection. This is great, but I want to avoid having to mouse to the paragraph styles panel, control-click and drag down to the menu selection. I figure this kind of style application should be scriptable, and "with next style" compiles in AppleScript... but without any apparent effect that I've noticed, and only [clearing overrides boolean] is shown in the scripting dictionary.

       

      Can this desired type of style application be scripted? If so, how? I assume I'll need a separate script for every first-paragraph style.

       

      Thanks.

        • 1. Re: Apply paragraph style with next style
          Kai Rübsamen Level 4

          Hi, I´m sorry that I´ve no code in Applescript for you, but in JS you can try something like this:

           

          p_styles = app.documents[0].paragraphStyles;
          cur_sel = app.selection[0].paragraphs;
          
          cur_sel.item(0).applyParagraphStyle (p_styles.item ("title"), true);
          cur_sel.item(1).applyParagraphStyle (p_styles.item ("title").nextStyle, true)
          

           

          ruebi

          • 3. Re: Apply paragraph style with next style
            Roy McCoy Level 1

            Thanks Kai. This doesn't do what I want - which is work on any number 

            of paragraphs, not just 0 and 1 as here, or 0 and 1 and 2 if I add 

            cur_sel.item(2) - but it does finally suggest to me how "with next 

            style" actually works, which is applying not the specified style 

            itself but its assigned next after style.

            • 4. Re: Apply paragraph style with next style
              Roy McCoy Level 1

              Yes.

              >

              http://in-tools.com/wordpress/indesign/plugins/formatting-tools-version-104-released

               

              Alas, my 30-day-demo has expired, and though Formatting Tools is still 

              in my Plug-Ins folder, InDesign doesn't recognize it. I used it only a 

              couple of times during the month and for some reason (inertia? 

              laziness? stupidity? too busy doing it the old way?) didn't get 

              adequately into it. One of the things that likely didn't work in its 

              favor was that though sophisticated scripting was evidently involved, 

              its application was still manual. I'm almost done with my current 108-

              article book and thus with its main job script (which has been 

              invaluably handy), but what I would now want to do, having acquired 

              experience with Next Style throughout the job, would be to incorporate 

              calls to Formatting Tools, or work its routines into my own script, so 

              that Next Style would be applied to a defined sequence of next-style 

              paragraph styles in the book. This would eliminate the need to invoke 

              Formatting Tools, or anything other than the main job script, manually.

               

              Thanks,

               

              Roy

              • 5. Re: Apply paragraph style with next style
                Kai Rübsamen Level 4

                Hello Roy,

                 

                I´m a beginner in scripting. The code I posted before works fine to me but it´s surely not a good solution for any purposes.

                 

                This one should be a little bit more flexible. In my test I had a title with next Style = subtitle and for the subtitle next Style = body and for body is the next Style = same Style

                 

                var style_name = "h1"
                
                var doc = app.documents[0];
                var first_pstyle = doc.paragraphStyles.item(style_name); 
                var cur_sel = app.selection[0].paragraphs;
                
                cur_sel[0].appliedParagraphStyle = first_pstyle; 
                
                if (cur_sel.length > 1)  
                     { 
                     for (n = 1; n < cur_sel.length; n++)
                          {
                          cur_sel[n].appliedParagraphStyle = cur_sel[n-1].appliedParagraphStyle.nextStyle; 
                          }
                     }
                

                cheers rübi

                1 person found this helpful
                • 6. Re: Apply paragraph style with next style
                  Roy McCoy Level 1

                  Okay, I created a small file with the text

                   

                  title

                  subtitle

                  body

                  body

                  body

                   

                  and the title style applied to the first line. I then ran your new 

                  script, which I had to fix up a little because it got slightly messed up

                  in the translation to e-mail text. And now it works!

                   

                  Let's see if I can get it into AppleScript...

                   

                  First of all, hmm, it's interesting that the script text in the forum

                  has

                   

                  var style_name = "h1"

                   

                  with "h1" red, while the e-mail version I received has

                   

                  var style_name = "title"

                   

                  Maybe you changed your message after posting it? No matter, I carry on

                  and here's the result:

                   

                  tell application "Adobe InDesign CS4"

                     tell document 1

                       set theStyle to paragraph style "1.Bold Head 1"

                       set applied paragraph style of paragraph 1 of selection to theStyle

                       repeat with i from 2 to count of paragraphs of selection

                         tell selection

                           set applied paragraph style of paragraph i to ¬

                             next style of applied paragraph style of paragraph (i - 1)

                         end tell

                       end repeat

                     end tell

                  end tell

                   

                  This works the same as yours with the "title" style, but let's go

                  one more:

                   

                  tell application "Adobe InDesign CS4"

                     tell document 1

                       set theStyle to paragraph style "1.Bold Head 1"

                       set applied paragraph style of paragraph 1 of selection to theStyle

                       repeat with i from 2 to count of paragraphs of selection

                         tell selection

                           set applied paragraph style of paragraph i to ¬

                             next style of applied paragraph style of paragraph (i - 1)

                         end tell

                       end repeat

                     end tell

                  end tell

                   

                  This works for any style, so I only need one script! Option-n is 

                  available as a QuicKeys trigger and I use option for other paragraph 

                  style assignments, so I'll assign it to that. Thanks!

                   

                   

                  Roy

                  • 7. Re: Apply paragraph style with next style
                    Roy McCoy Level 1

                    P.S.

                     

                    (1) It's ironic that I've gotten this figured out precisely after finishing the last of the 108 book articles for which I needed it. But I'm sure it will come in handy on other jobs later.

                     

                    (2) I said I wanted to put it into the main job script run at the beginning after importing the Word file, but this wouldn't be possible since with only a couple of scripted exceptions the paragraph styles aren't applied yet. I don't know if or how I'll ever put this into a secondary job script.

                    • 8. Re: Apply paragraph style with next style
                      AhmetSamsa

                      Hi Kai. I know this an old topic but script is still working. But its not working for styles in a group. Could you add an option for styles in group too?