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

Centering Left-aligned Text On Longest Line Per Page

Participant ,
Aug 13, 2017 Aug 13, 2017

I am working on a book of poems. I need to make sure that the (left aligned) text running within each page is centred on the longest line. As far as I am aware there is no mechanism for doing this automatically in inDesign and besides, the optical centre will always be different from the actual centre.

So I need to manually add an indent to every page to indent the text until it feels centred on the page. My problem is that some of the stanzas/paragraphs run between two pages which each need different indentation. I can only apply indent to a whole paragraph, meaning it will effect the beginning of the paragraph on one page and the end on another. If the second page requires a different indent this means the end of the paragraph that has run over from the previous page will indented correctly for the previous page and not the current page or vice-versa.

Is there any sensible solution to this problem?

5.9K
Translate
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 1 Correct answer

Community Expert , Aug 13, 2017 Aug 13, 2017

I've had some good fun setting poetry in the past. Most of it can be scripted. As Ariel and Bill mentioned, each line should be a separate paragraph. Then you can left-align the lines and centre the page on the longest line using the following script:

Select a text frame, then run the script

if (app.selection.length && app.selection[0] instanceof TextFrame) {

  frame = app.selection[0];

  longest = Math.max.apply (null, frame.lines.everyItem().endHorizontalOffset);

  indent = (frame.geometricBounds[3

...
Translate
New Here ,
May 31, 2022 May 31, 2022

I can't figure out how to delete this double comment—sorry, folks!

Translate
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
New Here ,
May 31, 2022 May 31, 2022

Hi there! I'm trying to use this script for a memoir I'm typesetting (with mixed content—some normal paragraph styles, with poems intersperced throughout). 

 

I changed the paragraph style to my own, but when I try to run the script, I'm getting a JavaScript Error:

Screen Shot 2022-05-31 at 12.25.28 PM.png

It seems like something in line 9 is causing the issue and it's probably a simple fix... but this is the ceiling of my JavaScript knowledge. Please help—thank you SO much!

 

Thanks! Pasting full code below:

/*

    0190_PoetryLines_MichelAllio.jsx
    based on an orginal code written by Peter Kahrel | See: https://forums.adobe.com/thread/2370094
    Written 2017/08/13

*/

app.doScript(main, ScriptLanguage.JAVASCRIPT, [], UndoModes.ENTIRE_SCRIPT, "Poetry Lines! …" );

function main()      
{
    var myDoc = app.activeDocument,
    myPages = myDoc.pages,
    P = myPages.length,  p;    

    for ( p = 0; p < P; p++ )
	{  
        var myFrames = myPages[p].allPageItems,
        F = myFrames.length,  f;

        for ( f = 0; f < F; f++ ) {
            var myFrame = myFrames[f];
			
            if ( myFrame instanceof TextFrame && myFrame.contents.length > 0 )
			{
               var longest = Math.max.apply (null, myFrame.lines.everyItem().endHorizontalOffset); 
               var indent = (myFrame.geometricBounds[3]/2) - (longest/2);
                myFrame.lines.everyItem().leftIndent = indent;
            }   
        }
    }

    var myParas = myDoc.stories.everyItem().paragraphs.everyItem().getElements();
    var P = myParas.length,  p;    
    for ( p = 0; p < P; p++ ) myParas[p].appliedParagraphStyle != myDoc.paragraphStyles.item("Poem Body–A (Ljust) START") && myParas[p].clearOverrides();
};
Translate
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 26, 2019 Nov 26, 2019

FWIW: I marked Peter Kahrel's post from Aug 13, 2017 as the Correct one.

Why? It contains the core code for solving this issue.

 

Regards,
Uwe Laubender

( ACP )

Translate
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
New Here ,
Apr 02, 2025 Apr 02, 2025

Dear gurus! I never would have thought that I would be writing in the same thread as the legendary Peter )))
I am having trouble with Uwe Laubender's script (also quite legendary). It worked perfectly until recently, and I was able to create a collection of poems quite quickly, thanks to you all. However, now, unfortunately, something has broken in it. It functions properly with a small number of poems or pages, but as soon as I add a significantly larger batch of poems, it tries to center them, then reverts the lines back to their original place and gives an error: Data is out of range (line 301).
Unfortunately, I don't understand anything about scripting, and I am asking for your help.
Thank you, and I apologize for the English; it's Google.

Translate
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 ,
Apr 02, 2025 Apr 02, 2025

Hi @popz29907986ftco ,

hm, is it perhaps the script code commented with this line:

0190_PoetryLines_MichelAllio.jsx

This code was not wriiten by me, it is @Obi-wan Kenobi 's one.

Without having a sample InDesign document at hand it's hard to tell what's going wrong.

Also important is the version of your InDesign.

 

Could you attach a sample document and give the URL of the post where the script code is posted?

NOTE: Attaching an InDesign document will only work if you are using the forum's editor and not when you answer by mail.

 

Regards,
Uwe Laubender
( Adobe Community Expert )

Translate
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
New Here ,
Apr 02, 2025 Apr 02, 2025

I work in the 17.4 Indesigne, running Windows 10. In the archive, work and script itself. I took the script from this thread about a year ago and it invariably helped me. What could go wrong? Thank you for responding and trying to help.

https://disk.yandex.ru/d/_txxM36Pqcp8_g

Translate
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 ,
Apr 02, 2025 Apr 02, 2025

Hi @popz29907986ftco ,

if you refer to the script file CenterLines.js that you provided with your packaged InDesign document, well, that's not the code @Obi-wan Kenobi provided in this thread.

 

Also: I see a lot of overset text in your document.

That could be the cause of failure of your script.

 

Regards,
Uwe Laubender
( Adobe Community Expert )

 

 

Translate
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
New Here ,
Apr 02, 2025 Apr 02, 2025

Oops. Absolutely true. Now I have compared the codes of two scripts and they are completely different. I'm sorry. Apparently I took this script in another place. Fine. Forget about him.

I have now copied Peter's code, inserted it into the NOTEPAD++ program and saved it as Pitercenter.js
Launched the entire text, no oversets, selected a text frame and launched PiterCenter.js
Unfortunately, I got an error. I would be very grateful for clarification.

Translate
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
LEGEND ,
Apr 02, 2025 Apr 02, 2025

@popz29907986ftco

 

You need to upload your Err-something screenshots again.

 

Translate
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
New Here ,
Apr 02, 2025 Apr 02, 2025
LATEST
 
Translate
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