Skip to main content
ashleyf97460441
Participant
May 17, 2016
Answered

Script to remove paragraph space before/after

  • May 17, 2016
  • 1 reply
  • 1221 views

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!!

This topic has been closed for replies.
Correct answer S Hopkins

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.appliedParagraphStyle.name;

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

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

      paraList.spaceAfter = 0;

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

        paraList.spaceAfter = 0;

     }

}

1 reply

S HopkinsCorrect answer
Inspiring
May 18, 2016

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.appliedParagraphStyle.name;

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

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

      paraList.spaceAfter = 0;

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

        paraList.spaceAfter = 0;

     }

}

ashleyf97460441
Participant
June 16, 2016

This works perfectly! Thank you!