4 Replies Latest reply on Feb 4, 2015 10:56 PM by JulianFT

    Resize text frames for chapter starts

    JulianFT Level 1

      Dear all,


      this one's a bit of a brainteaser for me, though no doubt I'm simply overlooking something silly.


      I have a document that has a manuscript placed into an autoreflowing text frame, with the chapters automatically separated using page breaks and headers with paragraph styles (in this case myPGS_CS). What I'm trying to do is resize the text frame on the first page of every chapter, so as to add a cover image above it. Now, the below code works fine until about four chapters before the end, when nothing happens anymore. I think this has something to do with the smaller text frames pushing the text to another page, and their header paragraphs somehow no longer being included in the paragraphs being searched. When I change the geometricBounds for myChapterStartPage.textFrames[0] to something minor (thus no longer pushing the text as far downwards); the last chapters are included and all chapter start pages have resized text frames.


      The solutions I tried involved somehow trying to 'refresh'  the allParas variable so as to include all the reflowed paragraphs, but I haven't figured out a way to do that properly yet. Any suggestions to do so, or any other solutions, would be immensely appreciated!


      Kind regards and thanks in advance,




      function allOverAgain(){
              var allParas = myDocument.stories.everyItem().paragraphs.everyItem().getElements();  
              for ( var p = 0; p < allParas.length; p++ ) { 
                      var pStyleName = allParas[p].appliedParagraphStyle.name;   
                      if (pStyleName === myPGS_CS.name) {
                          var chapterSubheading  = allParas[p];
                          var chapterHeading = allParas[p+1];
                          var chapterFirstPara = allParas[p+2];
                          var myChapterStartPage = myDocument.pages.item(chapterHeading.parentTextFrames[0].parentPage.name);
                      // Now, find the page number for the chapter start            
                      var myChapterStartPage = chapterHeading.parentTextFrames[0].parentPage;
                      myChapterStartPage.textFrames[0].geometricBounds = [170,606,1026,52];
                      myChapterStartPage.textFrames[0].textFramePreferences.insetSpacing = [60,45,88,45];    
                      //Then, add the cover image text frame on the current page.
                      myChapterImageTF = myChapterStartPage.textFrames.add();
                      myChapterImageTF.name = "chaptercover"
                      myChapterImageTF.geometricBounds = [0,663,300,0];
                      catch (e) {
          } // End allOverAgain function
        • 1. Re: Resize text frames for chapter starts
          Kai Rübsamen Adobe Community Professional

          Hi Julian,


          sometimes it’s better to loop backwards than forwards. But to find your error, it would be helpful to have your document.


          Apart from that, I would solve the task differently:

          1. Create two masterspreads with primary text frame enabled (since CS6)

          2. The primary text frame on masterspread two sits lower and the masterspread has also the placeholder for the chapter image

          3. So search for your paragraph and apply a different masterpage to the corresponding page

          That’s all.


          • 2. Re: Resize text frames for chapter starts
            JulianFT Level 1

            Hi Kai,


            thank you so much for your reply. Unfortunately, I'm currently limited to using CS5.5, so I don't suppose your solution would work.


            I've tried looping backwards before, but that seemed to alter the text frames on the wrong page. Might be I overlooked something there, though, so if you could give it a go that would be amazing.


            Files can be found here:

            Dropbox - Script test


            Thanks so much again, and kind regards,



            • 3. Re: Resize text frames for chapter starts
              Kai Rübsamen Adobe Community Professional

              Hi Julian,


              my workflow will only work in CS6 and above.


              If I tried your script, I had the same problems. Even if I use the loop for all pages instead of allParagraphs.

              First I think the script must be a bit quicker, if you check not every paragraph (more than 5000), instead I check in my version every page and assume, that the page has one textframe and the first para in this textframe must be my target.


              The problem for me in this case is 'smartTextReflow'. So I found here in the forum a line 'myDocument.recompse()' and it seems, that this solves the problem.


              Try this:

              app.doScript( allOverAgain, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Run the script!" );
              function allOverAgain() {
                  var myDocument = app.activeDocument;
                  var myPGS_CS = myDocument.paragraphStyles.item( "Chapter Subheading" );
                  var allPages = myDocument.pages; 
                  for ( var p = 0; p < allPages.length; p++ ) {
                      var curPage = allPages[p];
                      var tf = curPage.textFrames[0];
                      var firstPara = tf.paragraphs[0];     
                      if ( firstPara.appliedParagraphStyle.name != myPGS_CS.name ) {
                      var chapterSubheading  = firstPara;  
                      var chapterHeading = tf.paragraphs[1];  
                      var chapterFirstPara = tf.paragraphs[2];  
                      var myStartFrame = tf;  
                      myStartFrame.geometricBounds = [ 170 , 606 , 1026 , 52 ];  
                      myStartFrame.textFramePreferences.insetSpacing = [ 70 , 45 , 88 , 45 ]; 
                      //Then, add the cover image text frame on the current page. 
                      var targetPage = myStartFrame.parentPage;
                      var myChapterImageTF = targetPage.textFrames.add( {geometricBounds: [ 0 , 663 , 300 , 0 ] , name: "chaptercover" } );  
              } // End allOverAgain function  
              // end of the action
              var dlog = new Window( "palette" );
              dlog.size = [ 320 , 100 ];
              dlog.add( "statictext" , [ 80 , 100 , 320 , 116 ], "Done." );


              btw: Do you realize, that you can use margins in your doc and that every frame has a white fill and stroke > no good practice!




              • 4. Re: Resize text frames for chapter starts
                JulianFT Level 1

                That's perfect; recompose() was exactly what I was looking for! I do know that the script is far from efficient - in my desperation I've been playing around so much that I'm going to need to do some serious streamlining to get it all somewhat presentable again. ;-)


                I do realize fill and stroke on the text frames; later on in the same script the page background will be changing color, so I'd need the text frame to be separately colored to remain its legibility.


                In any case, thanks so much for your kind help, you've been an absolute life saver!