Skip to main content
Participant
March 17, 2025
Answered

Alignement à gauche et à droite dans un même paragraphe

  • March 17, 2025
  • 3 replies
  • 956 views

Bonjour,
pour les besoins d'un livre, je cherche une méthode qui me permettrait d'avoir la première ligne de chaque paragraphe alignée à gauche puis les suivantes alignées à droite et ce de façon automatique (sinon c'est trop facile).

Une idée ?

Merci par avance !

Correct answer m1b

Hi @gildasch3, sorry my quick script was terrible and didn't even handle single-line paragraphs. Also I should have explained that I expected right-aligned paragraph style—I really didn't explain anything, ugh!

 

But ignore all that because @Peter Kahrel's approach of just adding the right-aligned-tab to each line is much easier to understand so I have copied his better way. I have also added a seection that removes previous tabbing which is important because if you reflow the text (eg. after changing the column width or font size) you will need to run the script again and you don't want more and more added tabs!

 

See how this script goes.

- Mark

/**
 * @file Right Align Lines Except First.js
 *
 * 1. Select some left-aligned text.
 * 2. Run Script. Will add right-align tab at start of each
 *    line after the first, so that first line will be left-aligned
 *    and each line thereafter will be right-aligned.
 *
 * @author m1b (with help from Peter Kahrel)
 * @version 2025-03-18
 * @discussion https://community.adobe.com/t5/indesign-discussions/alignement-à-gauche-et-à-droite-dans-un-même-paragraphe/m-p/15215273
 */
function main() {

    if (0 === app.documents.length)
        return alert('Please select some text and try again.');

    var doc = app.activeDocument,
        text = doc.selection[0];

    if (
        !text
        || !text.hasOwnProperty('paragraphs')
        || 'function' !== typeof text.findGrep
        || 0 === text.paragraphs.length
    )
        return alert('Please select some text and try again.');

    // clean up previous attempts
    app.findGrepPreferences = NothingEnum.nothing;
    app.changeGrepPreferences = NothingEnum.nothing;
    app.findGrepPreferences.findWhat = '~y';
    app.changeGrepPreferences.changeTo = '';
    text.changeGrep();

    // get new reference
    text = doc.selection[0];

    var paragraphs = text.paragraphs;

    // justify right all lines except first
    for (var i = paragraphs.length - 1; i >= 0; i--) {

        if (!paragraphs[i].isValid)
            continue;

        linesLoop:
        for (var j = 1; j < paragraphs[i].lines.length; j++) {

            if (!paragraphs[i].lines[j].isValid)
                continue linesLoop;

            paragraphs[i].lines[j].insertionPoints[0].contents = SpecialCharacters.RIGHT_INDENT_TAB;

        }

    }

};
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Right Align Poetry');

Edit 2025-03-18: Bugfix: renewed `text` reference and added checks for invalid paragraphs.

3 replies

m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
March 18, 2025

Hi @gildasch3, sorry my quick script was terrible and didn't even handle single-line paragraphs. Also I should have explained that I expected right-aligned paragraph style—I really didn't explain anything, ugh!

 

But ignore all that because @Peter Kahrel's approach of just adding the right-aligned-tab to each line is much easier to understand so I have copied his better way. I have also added a seection that removes previous tabbing which is important because if you reflow the text (eg. after changing the column width or font size) you will need to run the script again and you don't want more and more added tabs!

 

See how this script goes.

- Mark

/**
 * @file Right Align Lines Except First.js
 *
 * 1. Select some left-aligned text.
 * 2. Run Script. Will add right-align tab at start of each
 *    line after the first, so that first line will be left-aligned
 *    and each line thereafter will be right-aligned.
 *
 * @author m1b (with help from Peter Kahrel)
 * @version 2025-03-18
 * @discussion https://community.adobe.com/t5/indesign-discussions/alignement-à-gauche-et-à-droite-dans-un-même-paragraphe/m-p/15215273
 */
function main() {

    if (0 === app.documents.length)
        return alert('Please select some text and try again.');

    var doc = app.activeDocument,
        text = doc.selection[0];

    if (
        !text
        || !text.hasOwnProperty('paragraphs')
        || 'function' !== typeof text.findGrep
        || 0 === text.paragraphs.length
    )
        return alert('Please select some text and try again.');

    // clean up previous attempts
    app.findGrepPreferences = NothingEnum.nothing;
    app.changeGrepPreferences = NothingEnum.nothing;
    app.findGrepPreferences.findWhat = '~y';
    app.changeGrepPreferences.changeTo = '';
    text.changeGrep();

    // get new reference
    text = doc.selection[0];

    var paragraphs = text.paragraphs;

    // justify right all lines except first
    for (var i = paragraphs.length - 1; i >= 0; i--) {

        if (!paragraphs[i].isValid)
            continue;

        linesLoop:
        for (var j = 1; j < paragraphs[i].lines.length; j++) {

            if (!paragraphs[i].lines[j].isValid)
                continue linesLoop;

            paragraphs[i].lines[j].insertionPoints[0].contents = SpecialCharacters.RIGHT_INDENT_TAB;

        }

    }

};
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Right Align Poetry');

Edit 2025-03-18: Bugfix: renewed `text` reference and added checks for invalid paragraphs.

gildasch3Author
Participant
March 18, 2025

@Peter Kahreland @m1b Thank you for your answers !
Unfortunaltely, I wasn't able to run those new scripts. As I said, it's my first time with script. I have the following window. Di you have an idea what's going on ?

m1b
Community Expert
Community Expert
March 18, 2025

Hi @gildasch3 you must save the script text as plain text, not rich text (rtf) because the formatting messes up the code. - Mark

Peter Kahrel
Community Expert
Community Expert
March 17, 2025

Select a text frame or click somewhere in the text, then try this:

p = app.selection[0].parentStory.paragraphs.everyItem().getElements();
for (var i = p.length-1; i >= 0; i--) {
  if (p[i].lines.length > 1) {
    p[i].lines[1].characters[0].insertionPoints[0].contents = 
      SpecialCharacters.RIGHT_INDENT_TAB;
  }
}

 

m1b
Community Expert
Community Expert
March 17, 2025

That an interesting problem @gildasch3. Off the top of my head I don't know how to solve it. Easiest way is to make a paragraph style "first line" and a paragraph for "the rest", but this might not be ideal in your case.

 

Also here is a super quick script that makes a hacky attempt, by adding a right-indent-tab character and a linefeed at the end of every first line. This script mostly works but will probably need to be refined.

- Mark

 

UPDATE: please don't bother with this code, it's terrible! I will post better code below.

 

function main() {

    if (0 === app.documents.length)
        return alert('Please select some text and try again.');

    var doc = app.activeDocument,
        text = doc.selection[0];

    if (
        !text
        || !text.hasOwnProperty('paragraphs')
        || 0 === text.paragraphs.length
    )
        return alert('Please select some text and try again.');

    text.paragraphs.everyItem().lines.firstItem().insertionPoints[-1].contents = SpecialCharacters.FORCED_LINE_BREAK;
    text.paragraphs.everyItem().lines.firstItem().insertionPoints[-3].contents = SpecialCharacters.RIGHT_INDENT_TAB;

}
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Force First Line Left');

 

 

gildasch3Author
Participant
March 17, 2025

Thank you Mark for your quick answer.
I don't really know about script on inDesign. It's a good oportunity to taste it. Unfortunatly, it don't work very well.
My document is a long poem running of about 156 pages. I'm afraid that I have to do it ligne after ligne.

I have A. I want B. With your script I obtain C.