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

edit an Indesign script that indents paragraphs to the last word of the preceding paragraph?

Participant ,
Nov 24, 2024 Nov 24, 2024

Copy link to clipboard

Copied

This question follows on from  a question on StackExchange in 2015 — to which @mdomino very kindly supplied a fantastic script that generates a contextual indent...

 

This script works well, unless my text frame and copy already has a left indent applied. I usually have a default left indent so that that lists, bullets etc can be set to hang outside the left margin. So it would be super useful to know how to adapt this  script to take into account any existing indent value.

In this example below, my text has an existing 40pt left indent. The script has done its thing and left-indented to the end of the preceding line — but ADDING the existing indent to the newly calculated indent value... (you can see the text BEFORE running the scrip at the bottom...)

fig.1  below shows text frame after running the script — indents have been added that also include the 40pt left margin

Zircon-Screenshot 2024-11-24 at 17.31.51.png

 

fig.2 below shows text frame beofre running the script —  with 40pt left margin

Zircon-Screenshot 2024-11-24 at 17.45.09.png

 

I am new to scripting and .jsx — if anyone knows how to adapt this script to take into account a global indent value that would be very helpful. I'm not sure where, for exampel, Id find the .jsx value that covers text indent.

It could be that this value is added manually to the script, or via a dialogue request...?

thanks for your time!

 

 

Heres the script in question, reproduced on the understandign that this was originally supplied by Timo Rychert

 

 

#target InDesign

var defaultIndent = 0;

//Checks if a text frame with 2 or more paragraphs was selected.
if (app.selection.length < 1 || !(app.selection[0].parentStory.paragraphs.length > 1)) {
  alert("Error\nSelect a text frame with at least 2 paragraphs and try again.");
  exit();
}

var story = app.selection[0].parentStory;
var paragraphs = story.paragraphs;

// loops through all paragraphs, checks the position of the last letter of the previous
// paragraph and sets the indent of the first line of the current paragraph accordingly.
for (var i = 1; i < paragraphs.length; i += 1) {

  // leaves the script, if the paragraph has moved out of the last text frame in the meantime.
  if(paragraphs[i].parentTextFrames.length === 0){
    break;
    }

  var prevChar = paragraphs[i - 1].characters[-2];
  var leftEdge = paragraphs[i].parentTextFrames[0].geometricBounds[1];
  var indentPos = prevChar.endHorizontalOffset - leftEdge;

  paragraphs[i].firstLineIndent = indentPos;

  // if the paragraph has moved out of the textframe it means that the first word
  // does not fit in the first line and indent will be reset to the default value.
  if(paragraphs[i].parentTextFrames.length === 0){
    paragraphs[i].firstLineIndent = defaultIndent;
  }
};

 

 

TOPICS
How to , Scripting

Views

219

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 2 Correct answers

Explorer , Nov 24, 2024 Nov 24, 2024

Please be more clear about what you want to do ("take into account"?).

Maybe you mean to change this line

 

paragraphs[i].firstLineIndent = indentPos;

 

to this?

 

paragraphs[i].firstLineIndent += paragraphs[i].firstLineIndent;

Votes

Translate

Translate
Community Expert , Nov 25, 2024 Nov 25, 2024

Ah, I see. Change this line:

 var leftEdge = paragraphs[i].parentTextFrames[0].geometricBounds[1];

 as follows:

 var leftEdge = paragraphs[i].parentTextFrames[0].geometricBounds[1] 
                 + paragraphs[i].leftIndent;

Votes

Translate

Translate
Explorer ,
Nov 24, 2024 Nov 24, 2024

Copy link to clipboard

Copied

Please be more clear about what you want to do ("take into account"?).

Maybe you mean to change this line

 

paragraphs[i].firstLineIndent = indentPos;

 

to this?

 

paragraphs[i].firstLineIndent += paragraphs[i].firstLineIndent;

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
Participant ,
Nov 25, 2024 Nov 25, 2024

Copy link to clipboard

Copied

@luis_guimaraes — yep, you managed to nail it there, and you understood my garbled question first time round! Thank you!

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
Explorer ,
Nov 25, 2024 Nov 25, 2024

Copy link to clipboard

Copied

LATEST

It did? I thought it didn't, and gave you another answer on stackoverflow.

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 ,
Nov 24, 2024 Nov 24, 2024

Copy link to clipboard

Copied

Your script works fine over here. Not sure why it doesn't work at your end. Maybe try with a new document.

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
Participant ,
Nov 24, 2024 Nov 24, 2024

Copy link to clipboard

Copied

Thanks for checking this question... I guess I wasn't clear enough...

 

if I run the script on a text box with NO overall indent it works well... see below

 

Zircon-Screenshot 2024-11-24 at 17.30.58.jpeg

 


 but if I run the script on a text box with pre-existing indents, the indent-to-end-of preceding paragraph 'overshoots' by the value of my global left indent...

 

I want to be able to set an overall left indent AND run the indenting script... does that make sense?

 

thanks!

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 ,
Nov 25, 2024 Nov 25, 2024

Copy link to clipboard

Copied

Ah, I see. Change this line:

 var leftEdge = paragraphs[i].parentTextFrames[0].geometricBounds[1];

 as follows:

 var leftEdge = paragraphs[i].parentTextFrames[0].geometricBounds[1] 
                 + paragraphs[i].leftIndent;

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
Participant ,
Nov 25, 2024 Nov 25, 2024

Copy link to clipboard

Copied

Many thanks @Peter Kahrel  — works well, as does @luis_guimaraes edit...

Really helpful, and makes me want to dig further into scripting!

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 ,
Nov 25, 2024 Nov 25, 2024

Copy link to clipboard

Copied

Oh dear -- I read @luis_guimaraes's suggestion earlier and forgot about it. Apologies.

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