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

Fixing leading by script

Contributor ,
Dec 05, 2024 Dec 05, 2024

Copy link to clipboard

Copied

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

Views

654

Translate

Translate

Report

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

Community Expert , Dec 05, 2024 Dec 05, 2024

@Rogerio5C09 

 

This should work: 

 

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

 

 

Votes

Translate

Translate
Community Expert , Dec 05, 2024 Dec 05, 2024
quote

And the spacing before? 🙂


By @James Gifford—NitroPress

 

doc.stories.everyItem().spaceBefore = "2mm"; 

Votes

Translate

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

Votes

Translate

Translate
Community Expert ,
Dec 06, 2024 Dec 06, 2024

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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? 

 

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

Did you try that?

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

@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

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

@Rogerio5C09

 

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

 

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

 

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

@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.

Votes

Translate

Translate

Report

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