Skip to main content
Inspiring
December 5, 2024
Answered

Fixing leading by script

  • December 5, 2024
  • 4 replies
  • 3029 views

Hi everyone! 

I have a document where the leading is not consistent across the paragraphs. I would like to set them all to "14.4.pt" and also add a 2 mm space before each paragraph. For that, I came up with the below the script, but for some reason nothing happens when I run it. Could anyone help me to figure out why? 

function fixLeading(){

var doc = app.activeDocument;

var leadingPoints = ["17pt","18pt","19pt","20pt"];
   
for (var i = 0; i < leadingPoints.length; i++) {
    app.findTextPreferences = app.changeTextPreferences = null;
    app.findTextPreferences.leading = leadingPoints[i];
    app.changeTextPreferences.leading = "14.4pt";
    app.changeTextPreferences.spaceBefore = "2mm";
    doc.changeText();
    }

}
This topic has been closed for replies.
Correct answer Peter Kahrel

@Rogerio5C09 -- Look no further than the two lines that @Robert at ID-Tasker suggested, repeated here for convenience:

 

 

doc.stories.everyItem().leading = '14.4pt';
doc.stories.everyItem().spaceBefore = '2mm';

 

 

 This is all you need. But for interests' sake, apart from the comments that you didn't call your function, the function is inefficient because it resets the find/change preferences at every iteration. Get these settings out of the for-loop. o your script would look as follows:

 

 

function fixLeading(){
  var doc = app.activeDocument;
  var leadingPoints = ["17pt","18pt","19pt","20pt"];
  app.findTextPreferences = app.changeTextPreferences = null;
  app.changeTextPreferences.leading = "14.4pt";
  app.changeTextPreferences.spaceBefore = "2mm";
  for (var i = 0; i < leadingPoints.length; i++) {
    app.findTextPreferences.leading = leadingPoints[i];
    doc.changeText();
  }
}

fixLeading();

 

 

By the way, why do you look for those four point sizes? Are they the leading values in your document? The two-liner doesn't care about existing leading, it simply sets all text to 14.4pt leading.

4 replies

Peter Kahrel
Community Expert
Peter KahrelCommunity ExpertCorrect answer
Community Expert
December 5, 2024

@Rogerio5C09 -- Look no further than the two lines that @Robert at ID-Tasker suggested, repeated here for convenience:

 

 

doc.stories.everyItem().leading = '14.4pt';
doc.stories.everyItem().spaceBefore = '2mm';

 

 

 This is all you need. But for interests' sake, apart from the comments that you didn't call your function, the function is inefficient because it resets the find/change preferences at every iteration. Get these settings out of the for-loop. o your script would look as follows:

 

 

function fixLeading(){
  var doc = app.activeDocument;
  var leadingPoints = ["17pt","18pt","19pt","20pt"];
  app.findTextPreferences = app.changeTextPreferences = null;
  app.changeTextPreferences.leading = "14.4pt";
  app.changeTextPreferences.spaceBefore = "2mm";
  for (var i = 0; i < leadingPoints.length; i++) {
    app.findTextPreferences.leading = leadingPoints[i];
    doc.changeText();
  }
}

fixLeading();

 

 

By the way, why do you look for those four point sizes? Are they the leading values in your document? The two-liner doesn't care about existing leading, it simply sets all text to 14.4pt leading.

m1b
Community Expert
Community Expert
December 5, 2024

Hi @Rogerio5C09, Robert and Brian noticed this, but I'm not sure if you understood. You have wrapped your code in a function `fixLeading`, with is fine, but nowhere in your code do you *call* the fixLeading function so nothing will happen when you run the code.

 

Add this line at the end:

fixLeading();
Robert at ID-Tasker
Legend
December 5, 2024

@Rogerio5C09

 

How/where do you call your function fixLeading()?

 

Because you've declared it - but when/where do you run/execute it? 

 

brian_p_dts
Community Expert
Community Expert
December 5, 2024

@Robert at ID-Tasker I hate when I forget main() at the end of the code!

James Gifford—NitroPress
Legend
December 5, 2024

This is a simple matter of adjusting the paragraph style/s. A script is genuinely not needed if you have any skill with ID layout at all.

 

This also appears to "fix" the problem as text overrides, not style changes, which is very, very poor practice.

 

An even poorer practice is to use scripts etc. as hacks to get around very basic proper methods. 

Inspiring
December 5, 2024

Hi @James Gifford—NitroPress, I totally agree with you, however believe me or not, the document was provided without paragraph styles, which makes things a bit more challenging 😞

Robert at ID-Tasker
Legend
December 5, 2024

@Rogerio5C09

 

If you can share your file with me - and let me know key properties by which you want to create styles - like FontFamily+FontStyle+PointSize, etc. - then I can style your file for you.