1 Reply Latest reply on May 14, 2014 12:15 PM by Jump_Over

    Adding Spreads, Text Frames, and Linking/Threading them... HELP!?


      Thank you for taking the time to few my post and trying to help me with this.

      I'd like to point out and a beginner when it comes to script, so thank you in advance for your patience and understanding.


      I'm working on a side project at work to try and help myself and other designers to improve workflow. The aim to to develop a script that will add 4 pages at a time (in spreads format / facing pages), with text frames that are linked/threaded.

      I know that inDesign already has Smart Text Reflow, but unfortunately due to how our templates are set up, this doesn't work the way it needs to. I wish changing our templates was an option, but it isn't.


      Below you'll find an example of how we work...


      Screen shot 2014-05-14 at 11.46.31.png


      As you can see, our templates are already set up in printers spreads with the inside pages containing a text frame with the inside front frame (1) being linked/threaded to the inside back frame (2).


      So this is the starting point...


      I've worked out that I can add new spreads using the following:


      doc = app.activeDocument


      The trouble I'm having at the moment is adding a text frame to each of the individual page within that spread.


      I can add a text frame by telling it which page to add it to...


      myDocument = app.activeDocument
      var myPage=myDocument.spreads.item(-1).pages.item(-1);
      var myPages= myDocument.spreads.add(LocationOptions.atEnd);
      //Assumes that the active document has a master page named "B-Master"
      //and at least three pages--page 3 is pages.item(2) because JavaScript arrays are zero-based.
      //Create a text frame on the current page.
      var myTextFrame = app.activeDocument.pages.item(4).textFrames.add();
      myTextFrame.geometricBounds = myGetBounds(myDocument, myPage);
      //myTextFrame.geometricBounds = [307, 410, 28.346, 28.346];
      function myGetBounds(myDocument, myPage){
      var myPageWidth = myDocument.documentPreferences.pageWidth;
      var myPageHeight = myDocument.documentPreferences.pageHeight
      if(myPage.side == PageSideOptions.leftHand){
      var myX2 = myPage.marginPreferences.left;
      var myX1 = myPage.marginPreferences.right;
      var myX1 = myPage.marginPreferences.left;
      var myX2 = myPage.marginPreferences.right;
      var myY1 = myPage.marginPreferences.top;
      var myX2 = myPageWidth - myX2;
      var myY2 = myPageHeight - myPage.marginPreferences.bottom;
      return [myY1, myX1, myY2, myX2];


      This adds to text frame to the left page of the spread. I'm unsure how to add it to the right page of the spread.

      Alongside this, I don't want to have to tell it which pages, for when the script is run again, it will simply add them to the same pages and not the new ones.

      So I'm assuming a loop might need to be used?


      After looking around I did find the following script that works, but not in spreads and I can't adjust it to work with spreads...


      // add a page at the end of the current document,
      // place a text frame on it, and link the text frame
      doc = app.activeDocument
      np = doc.pages.add();
      marg = np.marginPreferences;
      gb = [marg.top, marg.left,
        doc.documentPreferences.pageHeight - marg.bottom,
        doc.documentPreferences.pageWidth - marg.right];
      oldRuler = doc.viewPreferences.rulerOrigin
      doc.viewPreferences.rulerOrigin = RulerOrigin.pageOrigin;
      with( doc.pages[-1].textFrames.add() )
        geometricBounds = gb;
        previousTextFrame = doc.pages[-2].textFrames[0];
      doc.viewPreferences.rulerOrigin = oldRuler
      myItems = app.activeWindow.activeSpread.pageItems;



      I would love to be able to use this but for spreads instead.


      The trouble which this is it links/threads the text frame with the previous page, but as I mentioned before, our templates are set up in Printers Spreads, so the threading needs to be as shown below:


      Screen shot 2014-05-14 at 11.51.05.png


      I hope this makes sense. Please feel free to ask any questions you want. I hope together we can develop this script to how I need it to be.



        • 1. Re: Adding Spreads, Text Frames, and Linking/Threading them... HELP!?
          Jump_Over Level 5



          Let assume that you have created a doc with proper number of pages (with textFrames placed and sized ),

              which you have discovered already, I guess.

          Let assume that those textFrames are not threated (linked).

          Let assume that each "interior" page is applied to a specific masterSpread (let say "B").

          To connect textFrames in your way - you could run this code:

          // assummes that interior pages are applied to a specific masterSpread
          // (here it is a 2nd masterSpread ==> modify this if need)
          // assumes that  interior startPageNumber and endPageNumber is set
            mDoc = app.activeDocument,
            interiorMasterSpread = mDoc.masterSpreads[1],
            startPage = 3, endPage = 10,
            pageSum = endPage + startPage,
            pageCnt = endPage - startPage + 1,
            mInterior = [], mFrames = [],
            lowFrameNum, highFrameNum, currSpread,
            jumper, currFrame, prevFrame;
          //fill an array with interior pages
          for (var k = 0; k < mDoc.pages.length; k++)
            if (mDoc.pages[k].appliedMaster == interiorMasterSpread)
          // check if count of pages, their numbers and order is correct
          if (endPage <= startPage || pageSum % 2 == 0 || mInterior.length !=pageCnt) {
            alert ("Something wrong with pages");
          // assummes each page has one textFrame correctly sized and placed
          // each textFrame will be named here as algorythm says (there is a space up to 999 pages)
          // code will fill an array with textFrames names
          for (var k = 0; k < mInterior.length; k=k+2) {
            jumper = (k/2)%2;     // jumps between values 0 and 1
            lowFrameNum = startPage + k/2;
            highFrameNum = pageSum - lowFrameNum;
            currSpread = mInterior[k].parent;
               // page[0] or page[1]
            currSpread.pages[jumper].textFrames[0].name = ("00" + lowFrameNum).slice(-3);     // i.e "003"
            mFrames.push (currSpread.pages[jumper].textFrames[0].name);    
               // page[1] or page[0]
            currSpread.pages[Math.abs(jumper-1)].textFrames[0].name = ("00" + highFrameNum).slice(-3);     // i.e. "010"
            mFrames.push (currSpread.pages[Math.abs(jumper-1)].textFrames[0].name);
          mFrames.sort();     // sort ascending means proper order for linking
          // textFrames location is correct,
          // textFrames order in thread goes as an array order
          for (k = 1; k < mFrames.length; k++) {
            currFrame = mDoc.textFrames.item(mFrames[k]);
            prevFrame = mDoc.textFrames.item(mFrames[k-1]);
            currFrame.previousTextFrame = prevFrame;