5 Replies Latest reply on Sep 14, 2009 9:47 AM by johnBking78

    Find and change last paragraph style

    johnBking78 Level 1

      With .js or grep, I want to change the style of the last bullet paragraph to one with more space after, there are many of them in one story mixed with other styles.

       

      Change this:

       

      • blarr blarrblarr (bullet style)
      • blarr blarr blarr blarr (bullet style)
      • blarr blarr change this one (bullet style)

       

      To this:

       

      • blarr blarrblarr (bullet style)
      • blarr blarr blarr blarr (bullet style)
      • blarr blarr change this one (last bullet style)

       

       

      Can this be done? If anyone can help, it would be great!

       

       

      Many thanks!

        • 1. Re: Find and change last paragraph style
          Dave Saunders Level 4

          I reckon this JavaScript does the job. You might have to change the style names to match yours. Also, it assumes that the styles aren't grouped in a folder.

          //DESCRIPTION: Fix last bullet paragraphs
          
          (function() {
               if (app.documents.length > 0) {
                    findFixLastBullets(app.documents[0]);
               }
               function findFixLastBullets(aDoc) {
                    setupFindText();
                    app.findTextPreferences.appliedParagraphStyle =  "Bullet";
                    var myFinds = aDoc.findText();
                    for (var j = myFinds.length - 1; j >= 0; j--) {
                        var lastPara = myFinds[j].paragraphs[-1];
                        // if next para already BulletLast do nothing
                        var nextPara = lastPara.insertionPoints[-1].paragraphs[0];
                        if (nextPara.appliedParagraphStyle.name == "BulletLast") continue;
                        lastPara.appliedParagraphStyle = "BulletLast";
                    }
               }
               function setupFindText(find, change, wholeWd, caseSense, foots, hidLayers, lockedLayers, lockedStories, masters) {
               app.findTextPreferences = null;
               app.changeTextPreferences = null;
               try { app.findTextPreferences.findWhat = find } catch(e) {};
               try {app.changeTextPreferences.changeTo = change } catch(e) {};
               app.findChangeTextOptions.properties = {
                    caseSensitive:(caseSense == null ? false : caseSense),
                    wholeWord:(wholeWd == null ? false : wholeWd),
                    includeFootnotes:(foots == null ? false : foots),
                    includeHiddenLayers:(hidLayers == null ? false : hidLayers),
                    includeLockedLayersForFind:(lockedLayers == null ? false : lockedLayers),
                    includeLockedStoriesForFind:(lockedStories == null ? false : lockedStories),
                    includeMasterPages:(masters == null ? false : masters)
               }
          } // end setupFindText
          
          }())
          

           

          Notice the little touch that if the next paragraph is already BulletLast it ignores that found text. This allows you to run the script more than once if you add more text.

           

          Dave

          • 2. Re: Find and change last paragraph style
            johnBking78 Level 1

            Fantastic! Thank you very very much!

             

             

            Kind Regards,

             

             

            Adam

            • 3. Re: Find and change last paragraph style
              johnBking78 Level 1

              One quick question, if I wanted to change the paragraph style before the first bullet, but only if it was a specific style, is that possible?

               

              Example:

               

              Blarr blar paragraph text

              Blarr blar paragraph text (change this para)

              • Blarr blar paragraph text
              • Blarr blar paragraph tex
              • Blarr blar paragraph text
              • Blarr blar paragraph text

               

              Header text (No change)

              • Blarr blar paragraph tex
              • Blarr blar paragraph text
              • Blarr blar paragraph text


              Blarr blar paragraph text
              Blarr blar paragraph text

               

              I'm getting better at this, but this is just a bit beyond my capabilities.

               

              Thanks again!

              • 4. Re: Find and change last paragraph style
                Dave Saunders Level 4

                Yes, it's possible, but getting at the previous paragraph is a tad trickier than getting the next one. You can use JavaScript's "previousItem()" method but it tends to be rather slow on long documents. I tend to use:

                prevPara = para.parentStory.characters[para.index - 1].paragraphs[0];
                

                But that makes the assumption that para is not already the first paragraph. If there's a danger of that, you need somethiing like:

                 

                prevPara = para.index == 0 ? null : para.parentStory.characters[para.index - 1].paragraphs[0];

                And then you'd check for null, something like:

                if (prevPara !== null && prevPara.appliedParagraphStyle.name === "TriggerName") {
                   prePara.appliedParagraphStyle.name = "NewStyle"
                }

                If you use the same technique for finding the bulleted paragraphs, then para would be myFinds[j].paragraphs[0].

                 

                Dave

                1 person found this helpful
                • 5. Re: Find and change last paragraph style
                  johnBking78 Level 1

                  Again thank you very much! works a treat!