Skip navigation
fistaids
Currently Being Moderated

Page object

Nov 13, 2011 3:58 PM

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.

 
Replies
  • Currently Being Moderated
    Nov 13, 2011 6:29 PM   in reply to fistaids

    Try changing this line:

     

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

     

    To this:

     

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

     
    |
    Mark as:
  • John Hawkinson
    5,572 posts
    Jun 25, 2009
    Currently Being Moderated
    Nov 13, 2011 7:43 PM   in reply to fistaids

    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?

     
    |
    Mark as:
  • Currently Being Moderated
    Nov 13, 2011 7:56 PM   in reply to fistaids

    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.

     
    |
    Mark as:
  • John Hawkinson
    5,572 posts
    Jun 25, 2009
    Currently Being Moderated
    Nov 13, 2011 8:12 PM   in reply to fistaids

    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.

     
    |
    Mark as:
  • John Hawkinson
    5,572 posts
    Jun 25, 2009
    Currently Being Moderated
    Nov 14, 2011 9:48 PM   in reply to fistaids

    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.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points