Skip to main content
Inspiring
December 5, 2024
Answered

Fixing leading by script

  • December 5, 2024
  • 4 replies
  • 2996 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 KahrelCorrect answer
Adobe 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
Adobe 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
Brainiac
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
Adobe Expert
December 5, 2024

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

James Gifford—NitroPress
Brainiac
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 😞

brian_p_dts
Adobe Expert
December 5, 2024

Just to be clear, I'm not being negative or dismissing here except on the basis that some documents are so badly constructed that effort spent trying to "fix them up" is far less useful than fixing the faults in the first place  — or, whenever possible, making the original creator or author do it. I realize that's not always possible, especially if you're just an employee being told what to accomplish, but even for a paying client, there's a limit to how much wasted time you should invest. The right approach  — IMVHO — is to tell the client just how much work is really needed and let them decide if they want to fix it themselves, pay you a reasonable rate to do it (and reap the benefits of a much "sturdier" document)... or go find some other provider who will waste his/her time.

 

That's the best advice/help I can summon for what appears to be a real mess of a project. I try to do better when it's possible.


@James Gifford—NitroPress I disagree that using scripts is an even poorer practice to get around issues. A skilled scripter could probably very easily fix the leading / spaceBefore issues while also generating correct/ varied paragraph styles with very little effort once they've seen the doc. But even without a robust fix, sometimes hacks are necessary. The design field is crowded. It's very easy to say "make the client do it or pay you to do it", but that client could just as very easily go to Fiverr to find a hack to fix it manually. I have dealt with many poorly constructed documents in my time, and sometimes the correct solution is to hack your way through it.