9 Replies Latest reply on Mar 4, 2013 5:33 AM by shilpa25

    apply paragraphStyles  Inbetween paragraphStyles

    shilpa25 Level 1

      Hi Forum,

       

      this is the script im trying to change paragraphStyles[BodyText_Indent] of  paragraphs between "bullet" style and "bullet_2" style.

       

      only one instance is happenning after "bullet" style and before "bullet_2" style.

       

      please help!...

       

       

       

      myDoc = app.activeDocument;

      for (j = myDoc.stories.length - 1; j >= 0; j--) {

          myStory = myDoc.stories.item(j);

          myParas = myStory.paragraphs;

      //    if (myParas.length > 5) {

              for (i = 0; i <myStory.paragraphs.length;  i++) {

                  if (myStory.paragraphs[i].appliedParagraphStyle == myDoc.paragraphStyleGroups.item("Bullet").paragraphStyles.item("Bullet"))

                      myStory.paragraphs[i+1].appliedParagraphStyle = myDoc.paragraphStyleGroups.item("Bullet").paragraphStyles.item("Bullet_Indent")

                     

                      if (myStory.paragraphs[i].appliedParagraphStyle == myDoc.paragraphStyleGroups.item("Bullet").paragraphStyles.item("Bullet_2"))

                      myStory.paragraphs[i-1].appliedParagraphStyle = myDoc.paragraphStyleGroups.item("Bullet").paragraphStyles.item("Bullet_Indent");

      }}

        • 1. Re: apply paragraphStyles  Inbetween paragraphStyles
          Jump_Over Level 5

          Hi,

           

          My idea is:

          - use 'bullet' as a textFindPreferences.appliedParagraphStyle set;

          - story.findText() will create an array (let say StartArr) of those paragraphs;

          - use 'bullet2' as a textFindPreferences.appliedParagraphStyle set;

          - story.findText() will create an array (let say EndArr) of those paragraphs;

          we can assume both arrays are with same length, I think.

           

          - you could iterate through those arrays and catch a "start" and "end" paragraphs.

          - if 'start' paragraph would be a myStory.paragraphs.nextItem(StartArr[0].paragraphs[-1])

               ==> 'end' paragraph would be:  myStory.paragraphs.previousItem(EndArr[0].paragraphs[0]);

          - so your 1st part of text could be a part of story defined by
                    story.paragraphs.itemByRange(start,end).texts[0]

           

          and so on...

           

          watch this discussion:

          http://forums.adobe.com/thread/1162582?tstart=0

           

          rgds

          • 2. Re: apply paragraphStyles  Inbetween paragraphStyles
            shilpa25 Level 1

            thanks jumpover,

             

            but im little bit confused....

             

            ill try...

             

            thanks jumpover, thanks jumpover,

            • 3. Re: apply paragraphStyles  Inbetween paragraphStyles
              MrTIFF Level 3

              Great solution (and it works for me), but I'm not clear why you wrote:

               

                 ...nextItem(StartArr[0].paragraphs[-1])

               

              instead of just:

               

                 ...nextItem(StartArr[0])?

              • 4. Re: apply paragraphStyles  Inbetween paragraphStyles
                shilpa25 Level 1

                Hi stephen0218

                 

                Can you post the corrected one. please...

                thanks..

                • 5. Re: apply paragraphStyles  Inbetween paragraphStyles
                  MrTIFF Level 3

                  It's not 'corrected' -- it's just simplified a bit. But maybe it is a dangerous simplification for some reason. Jump_Over is much more of a scripting expert than I am. I think I'll wait for him to chime in ...

                  • 6. Re: apply paragraphStyles  Inbetween paragraphStyles
                    Jump_Over Level 5

                    Hi,

                     

                    stephen0218 wrote:

                     

                    ...but I'm not clear why you wrote:

                       ...nextItem(StartArr[0].paragraphs[-1])

                    instead of just:

                       ...nextItem(StartArr[0])?

                     

                    This is just in case continuos paragraphs as a StyleRange (more then one paragraph with chosen style).

                    Method .findText() is catching texts matching criteria, not paragraphs ==> so it could happens there is a few paragraphs collected in particular element of mFound array.

                    Thats why the last one is the one we are looking for.

                    If one para is collected ==> it is still the last one

                    • 7. Re: apply paragraphStyles  Inbetween paragraphStyles
                      shilpa25 Level 1

                      Hi JumpOver,

                       

                      As I m running short of time, could you please kindly,,, alter my  above script and post here,,, (genunly accepting, Im not so good in Array concept).

                       

                       

                      Ill be very helpful for me, to get this now... Greatfull to you...

                       

                       

                      thanks Jump Over.

                      • 8. Re: apply paragraphStyles  Inbetween paragraphStyles
                        Jump_Over Level 5

                        Hi,

                         

                        Here is a code:

                         

                        // each paraStyle "Bullet" has corresponding paraStyle "Bullet_2"
                        // paraStyle "BodyText_Indent" is applied to whole text inbetween those two; step by step, pair by pair...
                        //
                        var myDoc = app.activeDocument, 
                            myFirstPara, myLastPara, myBodyPara, myStory, 
                            mStartArr, mEndArr, startPara, endPara, currText;
                        
                        myFirstPara = myDoc.paragraphStyleGroups.item("Bullet").paragraphStyles.item("Bullet");
                        myLastPara = myDoc.paragraphStyleGroups.item("Bullet").paragraphStyles.item("Bullet_2");
                        // !!! watch this group name and style name !!!
                        myBodyPara = myDoc.paragraphStyleGroups.item("Body").paragraphStyles.item("BodyText_Indent");
                        
                        for (j = myDoc.stories.length - 1; j >= 0; j--) {
                            myStory = myDoc.stories.item(j);
                            mStartArr = sectionFinder (myStory, myFirstPara);
                            mEndArr = sectionFinder (myStory, myLastPara);
                            for (var k = 0; k<mStartArr.length; k++) {
                                startPara = myStory.paragraphs.nextItem(mStartArr[k].paragraphs[-1]);
                                endPara = myStory.paragraphs.previousItem(mEndArr[k].paragraphs[0]);    
                                currText = myStory.paragraphs.itemByRange(startPara,endPara).texts[0];
                                currText.appliedParagraphStyle = myBodyPara;
                                }
                            }
                        alert ("Done!");
                        
                        function sectionFinder (story, paraStyle) {
                            app.findTextPreferences = null;
                            app.findTextPreferences.appliedParagraphStyle = paraStyle;
                            return story.findText();
                        }
                        

                         

                        watch style names and styleGroups names;

                        enjoy:)

                        • 9. Re: apply paragraphStyles  Inbetween paragraphStyles
                          shilpa25 Level 1

                          What should I say,

                           

                          The functions, exhilarated me,,, stupendous,,..

                           

                          Greatfull to you, JumpOver..     thanks a lot for saving my energy and time....