2 Replies Latest reply on Jun 16, 2016 12:30 PM by ashleyf97460441

    Script to remove paragraph space before/after


      Hi! Looking for scripting help from the experts. I have to format lots of long documents with several heading styles and am looking for a way to apply local formatting to a heading depending on what follows. (Heading styles have space after so they are separated from body copy, however, if another heading follows, it looks better if that space is reduced - this is where some conditional javascript formatting might help).


      I need a script that can look for all Heading 1 followed by Heading 2, and then remove the after paragraph spacing from Heading 1. And so on with a Heading 3 that follows Heading 2, etc. I have enough knowledge to write all the variations but I am having trouble finding a starting point for this script, if there's a way to do it at all. I'm sure there is. Basically, it needs to look for all Paragraph Style X, and if that is followed by Paragraph Style Y, then set spacing after paragraph to 0. Can anyone here help?


      Thank you!!

        • 1. Re: Script to remove paragraph space before/after
          S Hopkins Adobe Community Professional

          This should get you started:

          Note stories.item(-1) will give you the first story if there is only one in addition to master story. Otherwise loop through your stories testing to make sure each story has more than 1 paragraph.


          var thisStyle, testStyle;

          var docRef = app.documents.item(0);

          var storyRef = docRef.stories.item(-1);

          var paraList = storyRef.paragraphs;

          var lastTest = paraList.length - 1;

          //repeat through array of paragraphs

          for (var i = 0; i < lastTest; i++) {

              thisStyle = paraList[i].appliedParagraphStyle.name;

              testStyle = paraList[i + 1].appliedParagraphStyle.name;

              if (thisStyle == "Heading1" && testStyle == "Heading2") {

                paraList[i].spaceAfter = 0;

              }else if (thisStyle == "Heading2" && testStyle == "Heading3") {

                  paraList[i].spaceAfter = 0;



          • 2. Re: Script to remove paragraph space before/after
            ashleyf97460441 Level 1

            This works perfectly! Thank you!