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.