Skip to main content
Olfar10263823
Inspiring
August 13, 2019
Answered

Dropped Lines

  • August 13, 2019
  • 1 reply
  • 996 views

I Have two paragraph styles: poem (this blue one), and poem-drop (this red-one)
I want to apply leftIndents to all of my poem-drops equal to the length of the previous poem or poem-drop paragraphs.


HAMLET

Did you not speak to it?

MARCELLUS

My lord!
             I did;
But answer made it none: yet once methought
It lifted up its head and did address
Itself to motion, like as it would speak;

So far I can only find the value of this indentation.

app.findGrepPreferences = app.changeGrepPreferences = null;

app.findGrepPreferences.findWhat = '\r';

app.findGrepPreferences.appliedParagraphStyle =  'poem';

var myFound = app.activeDocument.findGrep();

var i;

for (i = 0; i < myFound.length; i++) {

        my_relative_indent = myFound.insertionPoints[0].horizontalOffset;

        $.writeln(my_relative_indent);

}

Please give me a hint.

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

Thank you for your hint!

I I've changed it a little bit.

// please select a few paragraphs.

// leftIndent of the last paragraph will be matched to the first one.

myStartSelection = app.selection[0].paragraphs[0];

myEndSelection = app.selection[0].paragraphs[-1];

theStartXpos = myStartSelection.insertionPoints[-2].horizontalOffset - myStartSelection.insertionPoints[0].horizontalOffset + myStartSelection.leftIndent ;

myEndSelection.firstLineIndent = 0;

myEndSelection.leftIndent = theStartXpos;

This gives me some possibility to do it semi-automatically without tabs and with any number of dropped lines.

However, I'm still looking for the possibility to set it based on paragraph styles.

Is it possible to solve this problem by using find and replace?


Here's another approach: select a text frame and the script, below, processes the whole story:

p = app.selection[0].parentStory.paragraphs.everyItem().getElements();

for (i = 0; i < p.length; i++) {

  name = p.appliedParagraphStyle.name;

  if (name === 'poem') {

    x = p.lines[-1].insertionPoints[-2].horizontalOffset - p.lines[-1].parentTextFrames[0].geometricBounds[1];

  } else if (name === 'poem-drop') {

    p.leftIndent = x;

    x = p.lines[-1].insertionPoints[-2].horizontalOffset  - p.lines[-1].parentTextFrames[0].geometricBounds[1];

  }

}

If instead of handling the whole story you want to deal with selected paragraphs, then change the first line to

p = app.selection[0].paragraphs;

P.

1 reply

Jongware
Community Expert
Community Expert
August 13, 2019

Not a scripting solution, but: Indent-to-here at the end of the top line followed by a shift-Enter?

(A scripting solution would probably be something like setting the leftIndent value of that second line.)

Olfar10263823
Inspiring
August 13, 2019

Hi, Jongware

Unfortunately, the problem is that these must be paragraphs. And it's not always the second line.

Note that between 'Did you not speak to it?' and And 'My Lord!' there is one more quite different paragraph style with text: 'MARCELLUS'.

Paragraph style with 'My Lord!' should take the indentation from the paragraph style with text  'Did you not speak to it?'.

Jongware
Community Expert
Community Expert
August 13, 2019

Good call – I did not realize that. Perhaps add your indent value as a tabstop, then.

mySelection = app.selection[0].paragraphs[0];

theEndXpos = mySelection.insertionPoints[-2].horizontalOffset - mySelection.insertionPoints[0].horizontalOffset;

/* sneaky: get the next line by looking at the character after the return */

nextLine = mySelection.parentStory.insertionPoints[mySelection.insertionPoints[-1].index+1];

tabList = nextLine.tabStops;

tabList.everyItem().remove();

tabList.add({position:theEndXpos});

Top two lines: initial state. Bottom two lines: after running this script with the text cursor in the top of the two. (It doesn't mind the selection.)

Why insertionPoints[-2], you ask, to get the position of the last character? Because the very final character of a paragraph always is its hard return, and the position "beyond" that is on the start of the next line. This is the position "left' of the Return.

Why subtract InsertionPoints[0]? Because horizontalOffset is the actual x position of the cursor, in page coordinates. This subtracts the left margin from the 'current' position. Won't work nicely if you may have a LeftIndent and/or FirstLineIndent there; then you need to add those again.

Other than that it seems to work nicely.