Skip to main content
NooZ
Participating Frequently
June 3, 2021
Answered

how to shortcut the increase add space after and increase space before a paragraphi

  • June 3, 2021
  • 6 replies
  • 7101 views

I want to use two shortcuts to improve my work.

I think it is possible by scripting but i don't know how so i ask here.
 i want to create a shortcut to increase space after paraprgaph as the button in palette and one to increase the space before the paragraph.

 

my left mouse button and relative finger will thank you forever.

Correct answer Peter Kahrel

For many years I have used small scripts to add or remove space before or after paragraphs. That's much easier than creating separate paragraph styles.

 

Two scripts below: the first adds 1 pt to the space before the currently selected paragraph, the second one adds half a line of white before the current paragraph. 'Selected' means: just click somewhere in the paragraph.

 

I use slight variants of these script to decrease the space by 1 point (simple change += with -=), and to add/remove space after the selected paragraph (simply replace spaceBefore with spaceAfter).

 

Then assign these scripts to keyboard shortcuts. On my keyboard I use these keys:

F1: add 1 pt space before

Shift+F1: add half a line of white before

Ctrl+F1: remove 1 pt space before

F2: add 1 pt space after

Shift+F2: add half a line of white after

Ctrl+F2: remove 1 pt space after

 

These very simple scripts have saved me an enormous amount of time over the years.

 

P.

 

// Add 1 pt space before (to add e.g. 1 mm, use '1mm')

try {
  app.selection[0].spaceBefore += 1;
} catch (e) {
  alert (e.message);
}

 

// Add half a line of white before

function leading (p) {
  if (p.leading == Leading.AUTO) {
    return (app.selection[0].pointSize * p.autoLeading) / 100;
  }
  return p.leading;
}


try {
  par.spaceBefore += leading (par) / 2;
} catch (e) {
  alert (e.message);
}

 

6 replies

Peter Kahrel
Community Expert
Community Expert
October 30, 2023

@bracewell4213 

Don't bother. Below is an upgraded version of the script. It has two modes:

1. Select a text frame and all text columns are targeted.

2. Click in a text column, and only that column is targeted.

 

I also added something I remember I had in the script I used to have (but can't find), namely, a limit to the space to be added before the designated paragraphs. In the script you see the line

 

var maxAdd = 6;

 

which stipulates that not more than 6 points can be added to headings. Change that to any value you that meets your customer's specs.

 

 

// Select a frame to fill all columns in the frame
// Click in a text column to target just that column

(function () {

  app.scriptPreferences.measurementUnit = MeasurementUnits.POINTS;
  
  // Specify the names of the paragraph styles to target
  var headingStyles = ['SecA', 'SecB', 'SecC'];
  
  // Specify the maximum amount (in points)
  // that can be added before a style
  var maxAdd = 6;
  
  var re = new RegExp ('^(' + headingStyles.join('|') + ')$');
  var columns = [];
  var i, j;
  var frame, headings, gap, delta;

  // Return the headings in a column
  
  function findHeadings (column) {
    var a = [];
    var p = column.paragraphs.everyItem().getElements();
    for (var i = p.length-1; i >= 0; i--) {
      if (p[i] != column.paragraphs[0] 
          && re.test (p[i].appliedParagraphStyle.name)) {
        a.push (p[i]);
      }
    }
    return a;
  }

  if (app.selection.length === 0) {
    exit();
  }
  
  if (app.selection[0] instanceof TextFrame) {
    columns = app.selection[0].textColumns.everyItem().getElements();
    frame = app.selection[0];
  } else if (app.selection[0].hasOwnProperty ('baseline')) {
    columns = [app.selection[0].textColumns[0]];
    frame = app.selection[0].parentTextFrames[0];
  } else {
    exit();
  }

  for (i = 0; i < columns.length; i++) {
    
    headings = findHeadings (columns[i]);
    if (headings.length === 0) {
      continue;
    }

    gap = frame.geometricBounds[2] - columns[i].lines[-1].baseline;
    delta = gap / headings.length;
    if (delta <= maxAdd) {
      for (j = 0; j < headings.length; j++) {
        headings[j].spaceBefore += delta;
      }
    }
  }
}());

 

 

Known Participant
October 31, 2023

Thanks a ton, @Peter Kahrel !  When my current mayhem subsides, I will experiment with this! Very powerful.

Peter Kahrel
Community Expert
Community Expert
October 30, 2023

@bracewell4213 

Hold your horses, Carol. The script is very easy to change so that it targets a column rather than the whole frame.

Known Participant
October 30, 2023

@Peter Kahrel , that's excellent to hear. I have homework to do. 

Peter Kahrel
Community Expert
Community Expert
October 27, 2023

The script you mentioned was designed for a purpose. What you're after is to balance certain headings so that the baseline of the text frame's last line is on the frame's bottom. For that, too, I used a script. Here it is.

 

You specify the paragraph style names to target in the second line of the script.

 

To use the script, select the text frame and run the script. It determines the space between the bottom of the text frame and the last line's baseline, divides it by the number of headings on the page, and adds that to each paragraph's space-before. If a heading is at the top of the page it's ignored (because setting spaceBefore on the frame's first paragraph has no effect).

 

(function () {
    
  var headingStyles = ['SecA', 'SecB', 'SecC'];

  function findHeadings (p) {
    var a = [];
    var re = new RegExp ('^(' + headingStyles.join('|') + ')$');
    for (var i = p.length-1; i >= 0; i--) {
      if (p[i] != app.selection[0].paragraphs[0]
          && re.test (p[i].appliedParagraphStyle.name)) {
        a.push (p[i]);
      }
    }
    return a;
  }

  if (app.selection.length === 0 
      || !(app.selection[0] instanceof TextFrame)) {
    exit();
  }

  var headings = findHeadings (app.selection[0].paragraphs.everyItem().getElements());
  if (headings.length === 0) {
    exit();
  }

  var gap = app.selection[0].geometricBounds[2] 
              - app.selection[0].lines[-1].baseline;
  var delta = gap / headings.length;

  for (var i = 0; i < headings.length; i++) {
    headings[i].spaceBefore += delta;
  }
}());

 

Known Participant
October 29, 2023

Thanks, Peter! That's very interesting. It may motivate me to re-do some documents with two separate text frames for columns, so I can run it separately for each column. I look forward to further experimentation with this.

Best Regards,

Carol

Robert at ID-Tasker
Legend
October 29, 2023

I'm late to the party and maybe I'm also missing the point - but TextFrame can be justified vertically... 

 

Inspiring
October 27, 2023

I am using this:

https://youtu.be/P28LMcaRspk?si=De_UkMb1v8NFGloy

Can be adapt for paragraphs spacing, or combined with script what was posted here. 

Amazing useful btw 🙂

Known Participant
October 27, 2023

That is an interesting video. I don't really need anything but what I said above, adding 1pt or 2pt to selected paragraphs, but ideally exclusive by style. That's a lot to control, I realize. And I'm on a Mac, which makes so many things more difficult.  I would probably prefer to use a keyboard shortcut instead of mouse wheel, but it does sound interesting.

Peter Kahrel
Community Expert
Peter KahrelCommunity ExpertCorrect answer
Community Expert
June 3, 2021

For many years I have used small scripts to add or remove space before or after paragraphs. That's much easier than creating separate paragraph styles.

 

Two scripts below: the first adds 1 pt to the space before the currently selected paragraph, the second one adds half a line of white before the current paragraph. 'Selected' means: just click somewhere in the paragraph.

 

I use slight variants of these script to decrease the space by 1 point (simple change += with -=), and to add/remove space after the selected paragraph (simply replace spaceBefore with spaceAfter).

 

Then assign these scripts to keyboard shortcuts. On my keyboard I use these keys:

F1: add 1 pt space before

Shift+F1: add half a line of white before

Ctrl+F1: remove 1 pt space before

F2: add 1 pt space after

Shift+F2: add half a line of white after

Ctrl+F2: remove 1 pt space after

 

These very simple scripts have saved me an enormous amount of time over the years.

 

P.

 

// Add 1 pt space before (to add e.g. 1 mm, use '1mm')

try {
  app.selection[0].spaceBefore += 1;
} catch (e) {
  alert (e.message);
}

 

// Add half a line of white before

function leading (p) {
  if (p.leading == Leading.AUTO) {
    return (app.selection[0].pointSize * p.autoLeading) / 100;
  }
  return p.leading;
}


try {
  par.spaceBefore += leading (par) / 2;
} catch (e) {
  alert (e.message);
}

 

NooZ
NooZAuthor
Participating Frequently
June 3, 2021

Mr Peter Kahrel, You are a lifesaver!

jmlevy
Community Expert
Community Expert
June 3, 2021

I don't know anything about scripting but why don't you simply use paragraph styles?