11 Replies Latest reply on Nov 1, 2012 11:08 AM by Jump_Over

    script to get the first word on each page

    Paul1023 Level 1

      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.

        • 1. Re: script to get the first word on each page
          MatthewMariani Level 1

          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[i];
              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];
          
          • 2. Re: script to get the first word on each page
            Paul1023 Level 1

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

            • 3. Re: script to get the first word on each page
              MatthewMariani Level 1

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

              • 4. Re: script to get the first word on each page
                Paul1023 Level 1

                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?

                • 5. Re: script to get the first word on each page
                  MatthewMariani Level 1

                  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[i];
                      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];
                  
                  
                  1 person found this helpful
                  • 6. Re: script to get the first word on each page
                    Paul1023 Level 1

                    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?

                    • 7. Re: script to get the first word on each page
                      MatthewMariani Level 1

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

                       

                      What are the dimensions and orientation of your pages?

                      • 8. Re: script to get the first word on each page
                        Paul1023 Level 1

                        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.

                        • 9. Re: script to get the first word on each page
                          MatthewMariani Level 1

                          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?

                          • 10. Re: script to get the first word on each page
                            Paul1023 Level 1

                            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.

                            • 11. Re: script to get the first word on each page
                              Jump_Over Level 5

                              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[k].textFrames.item("bookmark").textVariableInstances[0].resultText;

                               

                              in chosen place on page[k-1].

                               

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