9 Replies Latest reply on Jun 16, 2011 11:20 PM by cfranklin8

    Duplicating previous page items on new page

    cfranklin8

      Hello--

       

      I'm new to InDesign scripting and am trying to write a script to add a new page immediately after the current page. I would also like to duplicate some of the text frames from the current page onto the new page.

       

      For example, I would finish working on page 8, then use the script to create a new page that would duplicate some of the content from page 8.

       

      Here is part of the script I've trying to write:

       

      var currPage = app.selection[0].parentTextFrames[0].parentPage;

      var sectionInfo = currPage.textFrames.itemByName("section");

       

      function newPage {

      var myPage = doc.pages.add(LocationOptions.after,currPage);
      myPage.appliedMaster  = doc.masterSpreads.item("A-2C");

      myPage.marginPreferences.columnCount = 2;
      myPage.marginPreferences.columnGutter = "3p0";
      var myFrame = myPage.textFrames.add({geometricBounds:["1p0", "32p", "3p", "63"]});

      myFrame.contents = sectionInfo;
      myFrame.label = "section";

      }

       

      Since I'm referencing the active page, any information I collect seems to disappear when I add a new page. I'm thinking of working the other way: create a new page and then reference information from the previous page. Again, I may be going about this wrong. Any and all suggestions are welcome and appreciated!

       

      c.

        • 1. Re: Duplicating previous page items on new page
          John Hawkinson Level 5

          What's an example of a variable containing information that is lost? It is best to be quite specific.

           

          You never call your newPage() function, but I assume you're actually calling it.

           

          You set sectionInfo to a textFrame, but you later assign it to myFrame.contents. That is, your are setting a variable that held a textFrame equal to the contents of a textFrame. That's probably not what you intended.

           

          Also, it's helpful to use syntax highlighting when posting scripts that are more than a line:

          http://forums.adobe.com/servlet/JiveServlet/showImage/2-2703004-23715/Screen+shot+2010-03-31+at+9.24.11+AM.png

          • 2. Re: Duplicating previous page items on new page
            cfranklin8 Level 1

            Hello John--

             

            Thanks for the reply!

             

            To be more specific, let me start with the document.

            • The script would be used by multiple writers/designers.
            • We have around 6 different master pages in this document--differentiating in where text frames are laid out.
            • Each page has a text frame that contains a topic header and a text frame that is essentially sub-header text (i.e., it explains the focus of that particular page). Depending on the master page, there are also 1-3 other frames on the page.
            • There are two possible paragraph styles which can be assigned to the header content and two paragraph styles which can be applied to the sub-header content. They do not appear anywhere else on the page. 
            • Text variables like chapter name or running header won't work unfortunately.

             

            Reason for the script:

            Let's assume I'm working on page 8 of a document that already contains 12 pages. Because the content won't all fit on that page, I need to add a new page after page 8. I wanted to create a script that would add a page immediately after page 8. The script would apply the appropriate master spread and text frames. Since the new page would have the exact same header/sub-header information, I wanted to populate text frames on page 9 with the header and sub-header information from page 8.

             

            My attempts at scripting:

            I was able to add the page immediately after the current page with the following statements:

             

            var currPage = app.selection[0].parentTextFrames[0].parentPage;
            var myPage = doc.pages.add(LocationOptions.after,currPage);
            

             

            This isn't ideal since I need to make sure the script is run while inside some text frame on the page. I added a try/catch to alert the script user that the selection needs to be at an insertion point. I'm not sure if there is a better way, but I can live with this if I have to.

             

            Adding the text frames and applying master spreads or layers wasn't a problem. My trouble really started when I tried to reference the header/sub-header text from the currPage and duplicate on the new page (myPage).

             

            I put script label on the frame containing the header text called "section." This I did manually for testing purposes. I would be adding the label to each new page created by the script.

             

            To reference that information:

            var sectionInfo = currPage.textFrames.itemByName("section");
            var sectionContent = sectionInfo.contents;
            

             

            This returned the correct information in the JavaScript Console.However, after adding the new page, I tried to call that information and received an error:

             

            var myFrame = myPage.textFrames.add({geometricBounds:["1p0", "32p", "3p", "63"]});
            myFrame.contents = sectionContent;

             

            The horrible orange errorline appeared through the "myFrame.contents = sectionContent;" statement. The page was created but none of the header text contents was brought into the new page.

             

            I don't know if this helps clear up my problem (that is, other than a clear lack of JavaScript knowledge). I'm not expecting anyone to write the script, but I'm hoping someone can point me in the right direction.

             

            Thank you all in advance for your help and insight!

             

            c.

            • 3. Re: Duplicating previous page items on new page
              John Hawkinson Level 5

              What version of InDesign are you using?

               

              This returned the correct information in the JavaScript Console.However, after adding the new page, I tried to call that information and received an error:

               

              var myFrame = myPage.textFrames.add({geometricBounds:["1p0", "32p", "3p", "63"]});
              myFrame.contents = sectionContent;
              

               

              The horrible orange errorline appeared through the "myFrame.contents = sectionContent;" statement. The page was created but none of the header text contents was brought into the new page.

              OK, you told me what line the error ocurred on, but you just seem to have failed to mention the text of the error message?

              • 4. Re: Duplicating previous page items on new page
                cfranklin8 Level 1

                I'm using CS5.

                 

                And now when I ran the script again to see the error message, the error "Object is invalid" occurred at the 3rd line:

                 

                var currPage = app.selection[0].parentTextFrames[0].parentPage;
                var sectionInfo = currPage.textFrames.itemByName("section");
                var sectionContent = sectionInfo.contents;
                
                • 5. Re: Duplicating previous page items on new page
                  John Hawkinson Level 5

                  I put script label on the frame containing the header text called "section." This I did manually for testing purposes. I would be adding the label to each new page created by the script.

                  var sectionInfo = currPage.textFrames.itemByName("section");

                   

                  That should not work in CS5. In CS5, itemByName() indexes against the object's Name (from hte layers panel), not the script label. How sure are you that it worked in the ESTK console?

                  • 6. Re: Duplicating previous page items on new page
                    cfranklin8 Level 1

                    Well, I can't say I'm absolutely sure. My method has been to test after each line was added, so I wouldn't move on until information was returned in the Console.  Nonetheless, your point makes me question that now (I've been so focused on what happens since the page was added).

                     

                    Now I'm not sure how to target that specific text frame. Since it has a paragraph style only applied to that text on any given page, is that something I should look at targeting somehow?

                     

                    Thank you so much!

                    • 7. Re: Duplicating previous page items on new page
                      John Hawkinson Level 5

                      It really depends on your application.

                      You can still use the script label, you just have to use a for loop over all textFrames on the page and compare the label property.

                      Or you could set the name in the layers panel, then your code would just work.

                      Or you could search for textframes that have paragraphs that match your paragraph style, but that could be annoying and slow if you have a lot of textframes.

                      • 8. Re: Duplicating previous page items on new page
                        cfranklin8 Level 1

                        I will try the layer option.

                         

                        Is this something only done through scripting? Or can you manually define the name of an object in the layers panel?

                         


                        Either way, I truly appreciate you taking the time to help me with this. 

                        • 9. Re: Duplicating previous page items on new page
                          John Hawkinson Level 5
                          I will try the layer option.

                          Is this something only done through scripting? Or can you manually define the name of an object in the layers panel?

                          Click on the object in the layes panel to select it, then click once again to focus the cursor on the name field and then edit it.