11 Replies Latest reply: Nov 14, 2011 10:18 PM by fistaids RSS

    Page object

    fistaids

      Hi all,

       

      I've just started messing with InDesign scripting, and being a web developer I've obviously chosen to go with Javascript as my scripting language.  I had been reading through the available literature and thought I had pretty much figured out the DOM, and writing the script I was after.

       

      The idea is, I've got 4 columns when the data flows top to bottom, left to right.  The function I wrote was supposed to add a textFrame to a page after first calculating how many frames are already on this page.  If it already contains 4 textFrames then it creates a new page, then puts the textFrame on that page.

       

      The issue I'm facing is that it works fine for page 1, and page 2... but when it adds a page to a spread (in this case, page 3), the page object returned (even when access the last page of the document independently app.activeDocument.pages[-1]), it is return the page 2 object... so putting the textFrame on this page.

       

      Below is my code.

       

       

      function addTextFrame()
      {
          var page = app.activeDocument.pages[-1];
          
          var thisPageFrames = page.textFrames.length;
          var columns = 4;
          
          if ( thisPageFrames == columns) {
              page = app.activeDocument.pages.add();
              thisPageFrames = 0;
          }
          var thisFrame = thisPageFrames + 1;
      
          var leftMargin = page.marginPreferences.left;
          var rightMargin = page.marginPreferences.right;
          var topMargin = page.marginPreferences.top;
          var bottomMargin = page.marginPreferences.bottom;
      
          var pageHeight = app.activeDocument.documentPreferences.pageHeight;
          var pageWidth = app.activeDocument.documentPreferences.pageWidth;
      
          var frameHeight = pageHeight - topMargin - bottomMargin;
          var frameWidth = (pageWidth - leftMargin - rightMargin) / columns;
          
          var frameRight = (thisFrame * frameWidth) + leftMargin;
          var frameBottom = frameHeight + topMargin;
          var frameTop = topMargin;
          
          var frameLeft = leftMargin;
          if (thisPageFrames != 0) {
              frameLeft += (thisPageFrames) * frameWidth;
          }
      
          var newFrame = page.textFrames.add();
          newFrame.geometricBounds = [frameTop, frameLeft, frameBottom, frameRight];
          
          return page;
      }
      
      addTextFrame();
      

       

      Any advice you can provide would help me soooo very much.

        • 1. Re: Page object
          Fred Goldman Community Member

          Try changing this line:

           

          page = app.activeDocument.pages.add();

           

          To this:

           

          page = app.activeDocument.pages.add(LocationOptions.atEnd);

          • 2. Re: Page object
            fistaids Community Member

            Unfortunately, the exact same result.

             

            Are you able to get the desired outcome by executing that script on your machine?

             

            My specs are:

            - OSX Lion

            - InDesign CS5

            - 2.7GHz i7

            - 8GB 1333 RAM

             

            Let me clarify by saying the page is actually being added to the end of the current document... that's not an issue.  However, I'm unable to access this page after it's added, and the app.activeDocument.pages.add() method returns a reference to the previous page object.

             

            Even attempting to access this page after the fact with app.activeDocument.pages[-1] takes me to the 2nd last page (left page of the spread).

            • 3. Re: Page object
              John Hawkinson Community Member

              The issue I'm facing is that it works fine for page 1, and page 2... but when it adds a page to a spread (in this case, page 3), the page object returned (even when access the last page of the document independently app.activeDocument.pages[-1]), it is return the page 2 object... so putting the textFrame on this page.

              Aer you sure it returns the page 2 object? That's not what happens to me. Instead, the problem is that the text frames you attempt to create on page 3 are given with coordinates that are actually on page 2. Because generally the origin for coordinates is the upper left corner of the spread not the page.

              But origin aside, coordinates are still spread-relative not page-relative.

               

              Did you single step through and check the value of page.name to draw your conclusion about the returned page?

              • 4. Re: Page object
                fistaids Community Member

                No, I had just assumed it was a reference to page 2.  Right, so the coordinates are relative to the SPREAD.  I was not aware of that.

                 

                So, how would one determine if we're currently on the right-hand side of a spread, and therefore adjust the coordinates to be X + the previous page dimensions?

                 

                I'm generating this for a magazine company and I don't have the full layout available to me yet so I need this to be a dynamic script (undefined number of pages as data is coming from XML feed being pumped in to the document).

                 

                I've actually just stumbled on the fact you can define columns inside a text frame so the columns idea I've done above is superfluous.   I just need to place one text frame on each subsequent page aligned to the margins and 100% of the available page size.

                • 5. Re: Page object
                  Fred Goldman Community Member

                  you can use the page.side property to determine if it is on the recto or verso side. Another way would be to check the page.documentOffset if it is an odd or even number.

                  • 6. Re: Page object
                    fistaids Community Member

                    I was intending on using the page number as a way to calculate it, but I'm unsure if their layout will start with a 2 page spread.  So, it would appear the page.side property is going to be my answer.

                     

                    You guys have been fantastic.  I have absolutely zero experience in the print industry and have only just started working with InDesign last week.  Thank you so much for your help.

                     

                    I'm probably going to have a bunch of other questions in the coming week or two.  Is this a good area to post those questions?

                    • 7. Re: Page object
                      John Hawkinson Community Member

                      No, I had just assumed it was a reference to page 2.  Right, so the coordinates are relative to the SPREAD.  I was not aware of that.

                      So, how would one determine if we're currently on the right-hand side of a spread, and therefore adjust the coordinates to be X + the previous page dimensions?

                      Actually, I think you're better off changing the coordinates to page-relative. See the Scripting Tutorial, Constructing a Document, which offers:

                       

                      myDocument.viewPreferences.rulerOrigin = RulerOrigin.pageOrigin;

                       

                      Sorry for misleading you. Why don't you autoflow the pages? I believe that works in scripting, but might be wrong.

                      • 8. Re: Page object
                        fistaids Community Member

                        Okay, that's probably a winner.  Just change the view preferences on the document.

                         

                        Autoflow?  I was making the assumption I could just calculate if the current text frame was being overflowed by the text I'm inserting, and if so, add a new page and text frame to the end of the document and move that text there.  Is this possible?

                         

                        Everything I've read suggests that InDesign doesn't want to add a new page to the end of the document, if importing XML through the usual means.  I'm importing a heavily nested XML document (which again appears to be difficult for InDesign to handle), so I'm doing everything with a script (iterating through multiple children and multiple nested loops and appending those to the text frame).

                        • 9. Re: Page object
                          fistaids Community Member

                          I've got a further question... of course.

                           

                          These XML documents will be served from a web server.  To ensure that people don't have multiple copies of these files on their machines, I've attempted to set up a script that will automatically import the XML document from a given URL each time it is activated.

                           

                          I'm able to manually open the file by using: -

                           

                          var xmlFile = File('Path/to/file.xml');

                           

                          However, doing the same with a URI in place of the local path returns nothing.

                           

                          var xmlFile = File('http://www.path.com/to/yourXML.xml');

                           

                          Any ideas?

                          • 10. Re: Page object
                            John Hawkinson Community Member

                            Very simply, it doesn't work that way.

                            The File constructor does not take a URL, nor does anything else.

                             

                            You can write a very simple HTTP client in JavaScript (there is an

                            example in the Javascript Tools guide, and Rorohiko offers a

                            better one for free), or you can call out to a platform-specific executable

                            (e.g. curl on the Mac) and have it download the file. Or you could

                            use a network filesystem mounted on your clients.

                            • 11. Re: Page object
                              fistaids Community Member

                              Thanks for that, John.  I did already have the answer.  I was using a socket to access the script and download the XML string based on an InDesign variable.

                               

                              The way I was using, though, was preventing me from accessing the entire contents.  I've since forehead slapped myself and sorted it out.

                               

                              Thanks again for your help.