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

script to get the first word on each page

Community Beginner ,
Oct 29, 2012 Oct 29, 2012

Is there a script that would make a list of the first word of each page (or, preferably, create a text box on each page and place the next page's word in that)? My document's all in one story with one text frame on each page, although there are several other frames (header, page number) applied through master pages.

TOPICS
Scripting
2.3K
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
Explorer ,
Oct 29, 2012 Oct 29, 2012

Something like this, maybe...

if( !(app.selection[0] instanceof InsertionPoint) ) {alert("First, target a story by inserting a blinking cursor."); exit(); }

var stry = app.selection[0].parentStory, d = app.documents[0], i, tf1, tf2, wrd, ntf, bnds, pg, ro = d.viewPreferences.rulerOrigin;

d.viewPreferences.rulerOrigin = RulerOrigin.SPREAD_ORIGIN;

for ( i = stry.textContainers.length - 1; i > 0;  i-- ) {

    tf2 = stry.textContainers;

    wrd = tf2.words[0];

    tf1 = stry.textContainers[i-1];

    pg = tf1.parentPage;

    bnds = pg.bounds;

    ntf = pg.textFrames.add( undefined, undefined, undefined, {geometricBounds:[ bnds[0], bnds[1], bnds[0] + "3p0", bnds[3] ] } );

    ntf.textFramePreferences.verticalJustification = VerticalJustification.BOTTOM_ALIGN;

    wrd.duplicate( LocationOptions.AT_BEGINNING, ntf );

    ntf.texts[0].justification = Justification.CENTER_ALIGN;

}

d.viewPreferences.rulerOrigin = RulerOrigin[ro];

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 Beginner ,
Oct 29, 2012 Oct 29, 2012

Thanks - it worked great. One follow-up, though - how would I change the position of the new text frames, and make them all linked?

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
Explorer ,
Oct 29, 2012 Oct 29, 2012

geometricBounds controls the size and location of the textframes. Where do you the text to appear?

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 Beginner ,
Oct 29, 2012 Oct 29, 2012

The text box is positioned at X: 10.0681 in, Y: 8.0425 in, and the size is W: 0.8638 in, H 0.085 in. Can it also assign a style?

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
Explorer ,
Oct 30, 2012 Oct 30, 2012

Landscape layout?

Assumes existence of paragraph style called "firstword" in main paragraph styles palette, not inside a paragraph style group.

if( !(app.selection[0] instanceof InsertionPoint) ) {alert("First, target a story by inserting a blinking cursor."); exit(); }

var stry = app.selection[0].parentStory, d = app.documents[0], i, tf1, tf2, wrd, ntf, otf, pg, ro = d.viewPreferences.rulerOrigin;

d.viewPreferences.rulerOrigin = RulerOrigin.PAGE_ORIGIN;

for ( i = stry.textContainers.length - 1; i > 0;  i-- ) {

    tf2 = stry.textContainers;

    wrd = tf2.words[0];

    tf1 = stry.textContainers[i-1];

    pg = tf1.parentPage;

    ntf = pg.textFrames.add( undefined, undefined, undefined, {geometricBounds:[ "8.0425in", "10.0681in", "8.1275in", "10.9319in"]} );

    ntf.textFramePreferences.verticalJustification = VerticalJustification.BOTTOM_ALIGN;

    wrd.duplicate( LocationOptions.AT_BEGINNING, ntf );

    ntf.texts[0].applyParagraphStyle( d.paragraphStyles.itemByName("firstword"), true );

    ntf.texts[0].insertionPoints[-1].contents = SpecialCharacters.FRAME_BREAK;

    if ( i < stry.textContainers.length - 1 ) ntf.nextTextFrame = otf;

    otf = ntf;

}

d.viewPreferences.rulerOrigin = RulerOrigin[ro];

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 Beginner ,
Oct 30, 2012 Oct 30, 2012

Something wasn't working with the styling - it kept freezing the program - so I removed the last four lines and kept the center_align line from the first version. That ran, but the boxes are in the wrong position.

Once you mentioned landscape layout, I realised why the numbers seemed odd - I have 'facing pages' turned on and had checked the position only on the right page. I changed the numbers to "8.0425in",  "4.5681in",  "8.1275in", "5.4319in" and it mostly worked - it placed the box at exactly 5 inches (X) and at 8.085 inches (Y) instead of 4.5681 inches. Any idea why?

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
Explorer ,
Oct 30, 2012 Oct 30, 2012

Did you create a paragraph style called "firstword" in the document?

What are the dimensions and orientation of your pages?

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 Beginner ,
Oct 30, 2012 Oct 30, 2012

I created the paragraph style.

The pages are portrait orientation, 5.5 x 8.5 inches. I also unchecked 'facing pages' before running the script last time.

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
Explorer ,
Oct 30, 2012 Oct 30, 2012

You wrote:  I changed the numbers to "8.0425in",  "4.5681in",  "8.1275in", "5.4319in" and it mostly worked - it placed the box at exactly 5 inches (X) and at 8.085 inches (Y) instead of 4.5681 inches. Any idea why?

No. I cannot reproduce the error you describe. I assume you've checked your numbers very closely in the script--I don't normally indicate measurements as strings.


you wrote: Something wasn't working with the styling - it kept freezing the program -

What do you mean by "freezing the program"? Is there a specific error message?

The paragraph style is named "firstword" and is all lowercase letters?

firstword paragraph style is NOT in a paragraph style group, right?

Is the first word on any page too long when formatted with the paragraph style "firstword" to fit in the text frame?

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 Beginner ,
Nov 01, 2012 Nov 01, 2012

I figured out what the problem was - I had the coordinates for the middle of the text box, rather than the upper left. I fixed that and it worked. It also wasn't freezing, just taking a really long time - I let it run for about an hour and it finally finished (it's about 700 pages).

Thanks for the script! Even with trying to get it to work, it's much faster than having to type in all the words manually.

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
Mentor ,
Nov 01, 2012 Nov 01, 2012

Hi Paul,

Did you consider using text variable (Match_Character_Style_Type) for catching first and last occurrence of some character style on a page?

This solution could keep dynamic connection to your content (works if text reflows)

Text variables could be added by master pages with proper size, geoBounds and style in a separated text box with its name, say, "bookmark".

Finally, if you would need first word from the next page placed on current page you could use a script. All it have to do is to place

myDoc.pages.textFrames.item("bookmark").textVariableInstances[0].resultText;

in chosen place on page[k-1].

That's only a hint from my side...

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 Beginner ,
Dec 10, 2024 Dec 10, 2024
LATEST

I'm reading this post and would like to do the same thing (that is, have the first word of the next page at the bottom of the current page). The script on this page appears to do that, but I honestly don't know what to do with the script. 1. Is there an easier way to do this in InDesign 20.0.1? 2. Can someone provide more specific directions of what to do with the script and then how to reference it in a box on the current page? Thanks.

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