8 Replies Latest reply on Apr 6, 2011 9:17 AM by xmlapi

    Overset text flow to next page using javascript

    xmlapi Level 1

      I have a document with some pages having overset text. Each page has 1 textframe. On pages with overset text, I want to duplicatate the page into a new page (should be the next page position) and then display only the overset text.If this duplicated page also has overset text, then the same thing needs to happen again.

       

      while( index < doc.pages.length)
          {

       

              var page = doc.pages.item(index);
              var textFrame = doc.textFrames.findItems({ label:  'mainText' })[0];

       

              if(textFrame != null && textFrame.overflows)
              {
                   page.duplicate(LocationOptions.after,page);
                  var newPage = templateDoc.pages.item(index+1);
                   var newFrame = newPage.textFrames.findItems({ label:  'lblMainText'})[0];
                  newFrame.contents = "";  
               }

       

      }

       

      The problem is that this seems to duplicate the pages out of order. Anyone have a better script (no plugins)?

        • 1. Re: Overset text flow to next page using javascript
          John Hawkinson Level 5

          Do you think we might need to see your .findItems() method? Also, I'm not so clear on the difference between doc and templateDoc.

           

          Anyhow, page.duplicate() returns the new page -- the result of the duplication process. So perhaps using that instead of your second .findItems() call will help.

           

          I think the real problem, though, is that you are adding pages to doc.pages as you are iterating over it. I think that ought to be OK, but it seems not to be? But you don't show the incrementing of index, so it's hard to know whether it is a problem specific to your code or not.

           

          If that's really your problem, then you can

           

          var pages = doc.pages.everyItem().index;

           

          and then iterate over the page indices (which will not change when you add pages). Of course then you'll have to get your added pages some other way...

          1 person found this helpful
          • 2. Re: Overset text flow to next page using javascript
            Marc Autret Level 4

            Also,

             

            newFrame.contents = "" is not safe. Use rather: newFrame.parentStory.contents = "";

             

            To link existingFrame to (empty) newFrame you can use: existingFrame.nextFrame = newFrame,

            but make sure that existingFrame is actually the last frame of the story!

             

            Be very careful with the pattern: myPage.duplicate(LocationOptions.after, myPage);

            (I often experienced unexpected results due to circular reference in my page specifier or something...)

             

            Finally, keep in mind that myPage.index property refers to the index of the page within its parent Spread

            (myPage.index is not myPage.documentOffset)

             

            • John, I'm not sure I'd follow you in:  var pages = doc.pages.everyItem().index;

            This typically returns an Array in the form [0, 0, 1, 0, 1, 0, 1, ... ], doesn't it??

             

            @+

            Marc

            • 3. Re: Overset text flow to next page using javascript
              Marc Autret Level 4

              Just a try:

               

              var doc = app.activeDocument,
                   pages = doc.pages,
                   LOC_AFTER = LocationOptions.after,
                   i, p, t0, t1;
              
              // Note: don't cache pages.length, recalculation needed!
              for( i=0 ; i < pages.length ; ++i )
                   {
                   // Get and check the 1st text frame of the page
                   // ---
                   t0 = (p=pages[i]).textFrames[0];
                   if( !t0.isValid || !t0.overflows ) continue;
                   
                   // Safe duplication --I hope!
                   // because: p.duplicate(LOC_AFTER,p) is NOT SAFE
                   // ---
                   p = p.duplicate().move(LOC_AFTER,p);
                   
                   // Target and empty the new frame
                   // ---
                   (t1 = p.textFrames[0]).parentStory.contents = '';
                   
                   // Link the frames
                   // ---
                   t1.previousTextFrame = t0;
                   }
              
              

               

              Hope that helps.

               

              @+

              Marc

              1 person found this helpful
              • 4. Re: Overset text flow to next page using javascript
                John Hawkinson Level 5

                John, I'm not sure I'd follow you in:  var pages = doc.pages.everyItem().index; This typically returns an Array in the form , doesn't it??

                 

                Yikes! Typo. I meant id, not index:

                 

                >> app.activeDocument.pages.everyItem().id
                159,58903,58330,58336,58337,58910,58916,58917
                

                 

                • 5. Re: Overset text flow to next page using javascript
                  Marc Autret Level 4

                  Slight improvement:

                   

                  var LOC_AFTER = LocationOptions.after,
                       LOC_END = LocationOptions.atEnd;
                  
                  var doc = app.activeDocument,
                       pages = doc.pages,
                       n = pages.length - 1,
                       i, p, t0, t1;
                  
                  app.scriptPreferences.enableRedraw = false;
                  
                  for( i=0 ; i <= n ; ++i )
                       {
                       t0 = (p=pages[i]).textFrames[0];
                       if( !t0.isValid || !t0.overflows ) continue;
                       
                       p = i==n ?
                            p.duplicate(LOC_END):
                            p.duplicate().move(LOC_AFTER,p);
                       ++n;
                       
                       (t1 = p.textFrames[0]).parentStory.contents = '';
                       t1.previousTextFrame = t0;
                       }
                  
                  app.scriptPreferences.enableRedraw = true;
                  

                   

                   

                  @+

                  Marc

                  • 6. Re: Overset text flow to next page using javascript
                    Harbs. Level 6

                    Marc Autret wrote:

                         t1.previousTextFrame = t0;
                    

                     

                     

                    I seem to remember there being a difference between t1.previousTextFrame = t0 and t0.nextTextFrame = t1.

                     

                    What I remembered was that in the second case, the resulting story is the t0 story, while in the first case, the resulting story was the t1 story.

                     

                    I just tested in CS5 and found this to not be true. In both cases, the resulting story was the t0 one. I don't know if this has been changed at some point (I think my original testing was done in CS or CS2.) If it has been changed, setting the nextTextFrame is safer...

                     

                    Harbs

                    • 7. Re: Overset text flow to next page using javascript
                      paolbot

                      I use this:

                       

                      function flow(){

                          for(var i = 0; i < app.activeDocument.pages.count(); i++)

                          {

                              for(var b = 0; b < app.activeDocument.pages[i].textFrames.count(); b++)

                              {

                                  if(app.activeDocument.pages[i].textFrames[b].overflows == true )

                                  {

                                      var currentPage = myDocument.pages[i];

                                      var myPageNo = currentPage.name;

                                      var currentFrame=currentPage.textFrames[b];

                                      var e =currentFrame.geometricBounds[0];

                                      var f =currentFrame.geometricBounds[1];

                                      var g =currentFrame.geometricBounds[2];

                                      var h =currentFrame.geometricBounds[3];

                       

                                      if(myPageNo %2==0){

                                          var myMargins =currentPage.marginPreferences;

                                          var a = currentPage.bounds[0]+myMargins.top;

                                          var b = currentPage.bounds[1]+myMargins.right;

                                          var c = currentPage.bounds[2]-myMargins.bottom;

                                          var d = currentPage.bounds[3]-myMargins.left;

                                          }

                       

                                      else{

                                          var myMargins =currentPage.marginPreferences;

                                          var a = currentPage.bounds[0]+myMargins.top;

                                          var b = currentPage.bounds[1]+myMargins.left;

                                          var c = currentPage.bounds[2]-myMargins.bottom;

                                          var d = currentPage.bounds[3]-myMargins.right;

                                          }

                                      if (g < c)

                                      {

                                          currentFrame.geometricBounds = [a,b,c,d];

                                          }

                                      else if (g==c){

                                          }

                                      var newPage = myDocument.pages.add(LocationOptions.AFTER, currentPage);

                                      currentPage = newPage

                                      myPageNo = currentPage.name

                       

                                      if(myPageNo %2==0){

                                          var myMargins =currentPage.marginPreferences;

                                          var a = currentPage.bounds[0]+myMargins.top;

                                          var b = currentPage.bounds[1]+myMargins.right;

                                          var c = currentPage.bounds[2]-myMargins.bottom;

                                          var d = currentPage.bounds[3]-myMargins.left;

                                          var newFrame = currentPage.textFrames.add({geometricBounds:[a,b,c,d]});

                                          newFrame.textFramePreferences.textColumnCount = 7;

                                          newFrame.previousTextFrame = currentFrame;

                                          }

                       

                                      else{

                                          var myMargins =currentPage.marginPreferences;

                                          var a = currentPage.bounds[0]+myMargins.top;

                                          var b = currentPage.bounds[1]+myMargins.left;

                                          var c = currentPage.bounds[2]-myMargins.bottom;

                                          var d = currentPage.bounds[3]-myMargins.right;

                                          var newFrame = currentPage.textFrames.add({geometricBounds:[a,b,c,d]});

                                          newFrame.textFramePreferences.textColumnCount = 7;

                                          newFrame.previousTextFrame = currentFrame;

                                          }

                                      }

                                  }

                              }

                          }

                       

                      Hope help you

                      • 8. Re: Overset text flow to next page using javascript
                        xmlapi Level 1

                        Wow, thanks for the explanations. myPage.duplicate was causing the issue and as soon as I did the move thing, it worked! Thanks!