10 Replies Latest reply on Nov 11, 2008 7:39 PM by (Simon_Paterson)

    [CS3 AS] set paragraph style, go to next paragraph, set paragraph style, etc.

    henrikstrmberg
      hello,
      i'm pretty much a noob at scripting. i'd like to automate applying paragraph styles to a document with reviews.

      basically, i want to place the cursor in a paragraph (manually), and have a script do the following: set paragraph style 5, go to the next paragraph, set paragraph style 1, go to the next paragraph, set paragraph style 2, go to the next paragraph, set paragraph style 3.

      help?
      (i specified AS as i think i might understand it, but JS probably works as well)
        • 1. Re: [CS3 AS] set paragraph style, go to next paragraph, set paragraph style, etc.
          Level 1
          You don't need a script for this. Just use Next Style in the paragraph styles to set up the relationship, select the paragraphs and use Apply with Next Style from the Paragraph Style panel drop-down menu.

          To see how to step through paragraphs in a JavaScript, go here:

          http://jsid.blogspot.com/search?q=insertionPoints[-1].paragraph

          and scroll down to the third (or so) item called "Last shall be First versus NextItem"

          Dave
          • 2. Re: [CS3 AS] set paragraph style, go to next paragraph, set paragraph style, etc.
            henrikstrmberg Level 1
            yes, but then i'd have to select the paragraphs (rather than just stepping down to the first line) and go to the Paragraph Style panel drop-down menu and select Apply with Next Style (which i apparently cannot assign a shortcut to).

            I think i get some of the stuff in your link, but mostly not.
            • 3. Re: [CS3 AS] set paragraph style, go to next paragraph, set paragraph style, etc.
              (Simon_Paterson) Level 1
              Assuming you set the relationship of one style to the next using the 'Next Style' definition in the paragraph styles, then the following should do the trick (assuming you have your cursor in the first paragraph).

              > // Follows next paragraph style setting until Same Style or No Paragraph Style

              >// Get the current pagaraph based on user cursor location

              var myCurrentParagraph = app.documents[0].selection[0].paragraphs[0];

              do

              ] {

              ] // Currently applied paragraph style

              var myCurrentParagraphStyle = myCurrentParagraph.appliedParagraphStyle;

              ] // Next Style for current paragraph style

              var myNextParagraphStyle = myCurrentParagraph.appliedParagraphStyle.nextStyle;

              ] // Check current style and next style are not the same or No Paragraph Style

              if (myCurrentParagraphStyle == myNextParagraphStyle

              || myNextParagraphStyle == app.documents[0].paragraphStyles.item("[No Paragraph Style]") ) {

              ]] exit(); }

              ] // Get next paragraph in chain

              var myNextParagraph = myCurrentParagraph.insertionPoints[-1].paragraphs[0];

              ] // If we are end of story then stop

              if (myNextParagraph == myCurrentParagraph) { exit(); }

              ] // Apply next style to next paragraph

              myNextParagraph.appliedParagraphStyle = myNextParagraphStyle;

              ] // Restart the process

              myCurrentParagraph = myNextParagraph;

              ] }

              >while (true)


              Note that the process will end when the Next Style setting of one paragraphs is Same Style or when the Next Style is No Paragraph Style. There would be other ways to end the process, such as limiting it to a certain number of steps if the number of steps was always the same. Let me know.
              • 4. Re: [CS3 AS] set paragraph style, go to next paragraph, set paragraph style, etc.
                (Simon_Paterson) Level 1
                Oops, just realised you said AS is better. Here is the same thing in AS, but without the comments:

                >tell application "Adobe InDesign CS3" to tell document 1

                ] set myCurrentParagraph to object reference of paragraph 1 of item 1 of selection

                ] repeat

                ]] set myCurrentParagraphStyle to applied paragraph style of myCurrentParagraph

                ]] set myNextParagraphStyle to next style of applied paragraph style of myCurrentParagraph

                ]] if myCurrentParagraphStyle is equal to myNextParagraphStyle then exit repeat

                ]] set myNextParagraph to object reference of paragraph 1 of insertion point -1 of myCurrentParagraph

                ]] if myNextParagraph is equal to myCurrentParagraph then exit repeat

                ]] set applied paragraph style of myNextParagraph to myNextParagraphStyle

                ]] set myCurrentParagraph to myNextParagraph

                ] end repeat


                >end tell
                • 5. Re: [CS3 AS] set paragraph style, go to next paragraph, set paragraph style, etc.
                  henrikstrmberg Level 1
                  thanks!
                  that works great!

                  if i want the script to set the first paragraph to a specific style (i.e. "byline") instead of the current style, how would i do that?
                  • 6. Re: [CS3 AS] set paragraph style, go to next paragraph, set paragraph style, etc.
                    (Simon_Paterson) Level 1
                    No problems. Just add this line after the 'repeat' line:

                    >set applied paragraph style of myCurrentParagraph to paragraph style "byline"

                    Good luck,
                    Simon
                    • 7. Re: [CS3 AS] set paragraph style, go to next paragraph, set paragraph style, etc.
                      henrikstrmberg Level 1
                      yes!
                      I changed it to
                      b before
                      the 'repeat' line, and it works beautifully.

                      is there any way to also make the script move the cursor to the last line it changed - or, simply, move it down 4 paragraphs?

                      thanks,
                      hs
                      • 8. Re: [CS3 AS] set paragraph style, go to next paragraph, set paragraph style, etc.
                        (Simon_Paterson) Level 1
                        >I changed it to before the 'repeat' line, and it works beautifully.

                        Yes, silly me, that is exactly what I meant to say. Well done.

                        >is there any way to also make the script move the cursor to the last line it changed - or, simply, move it down 4 paragraphs?

                        Add this line at the end of the script
                        i before
                        (it should be right this time) 'set myCurrentParagraph to myNextParagraph' line:

                        >set selection of document 1 to insertion point 1 of myNextParagraph

                        If you want the cursor to be at the end of the line, rather than the beginning, use:

                        >set selection of document 1 to insertion point -2 of myNextParagraph
                        • 9. Re: [CS3 AS] set paragraph style, go to next paragraph, set paragraph style, etc.
                          henrikstrmberg Level 1
                          thank you for all your help!
                          here's the complete script, if anyone's interested:

                          > tell application "Adobe InDesign CS3" to tell document 1
                          set myCurrentParagraph to object reference of paragraph 1 of item 1 of selection
                          set applied paragraph style of myCurrentParagraph to paragraph style "byline"
                          repeat
                          set myCurrentParagraphStyle to applied paragraph style of myCurrentParagraph
                          set myNextParagraphStyle to next style of applied paragraph style of myCurrentParagraph
                          if myCurrentParagraphStyle is equal to myNextParagraphStyle then exit repeat
                          set myNextParagraph to object reference of paragraph 1 of insertion point -1 of myCurrentParagraph
                          if myNextParagraph is equal to myCurrentParagraph then exit repeat
                          set applied paragraph style of myNextParagraph to myNextParagraphStyle
                          set myCurrentParagraph to myNextParagraph
                          end repeat
                          set selection to insertion point 1 of myNextParagraph
                          end tell