Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Fixing leading by script

Contributor ,
Dec 05, 2024 Dec 05, 2024

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();
    }

}
TOPICS
How to , Scripting
3.6K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 3 Correct answers

LEGEND , Dec 05, 2024 Dec 05, 2024

@Rogerio5C09 

 

This should work: 

 

doc.stories.everyItem().leading = "14.4pt"; 

 

 

Translate
LEGEND , Dec 05, 2024 Dec 05, 2024
quote

And the spacing before? 🙂


By @James Gifford—NitroPress

 

doc.stories.everyItem().spaceBefore = "2mm"; 
Translate
Community Expert , Dec 05, 2024 Dec 05, 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:

 

 

f
...
Translate
Community Expert ,
Dec 06, 2024 Dec 06, 2024

What you enter in the Find what and Change to fields.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 06, 2024 Dec 06, 2024
quote

What you enter in the Find what and Change to fields.


By @Peter Kahrel

 

But it can be run globally on the whole document? 

 

Or am I missing something? 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 06, 2024 Dec 06, 2024

Did you try that?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 06, 2024 Dec 06, 2024

@Peter Kahrel I tried it just now and it worked as I expected, on multiple stories in the document. Are we talking about the same thing?

- Mark

Screenshot 2024-12-06 at 21.55.29.png

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 06, 2024 Dec 06, 2024

Ah, like that. But then you'll have to run it for 17, 18, 19, and 20 pts separately because you have to have something in the Find what field and/or the Find Format panel.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 06, 2024 Dec 06, 2024

Yep exactly! Easily done in 30 seconds. Just a funny observation.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 06, 2024 Dec 06, 2024
LATEST

Funny indeed! Still, you'd have to hope that those four are the only point sizes used. But I take your point, a good one.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 05, 2024 Dec 05, 2024

@Rogerio5C09

 

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

 

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

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 05, 2024 Dec 05, 2024

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 05, 2024 Dec 05, 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();
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 05, 2024 Dec 05, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines