Search code examples
scriptingadobe-indesignextendscriptindesign-server

Is there a way to tell which page number the textframe exists in?


I am trying to find out whether a particular text exists and perfectly fits in the text frame and when the text flows to the next text frame, then upon calculating the coordinates(baseline) of the next frame iam increasing the size of the existing frame and trying to fit the text but when the text frame is in the next page then the baseline it's giving me a negative value so I would like to know if there is any way I could understand that the next text frame is in next page so as to avoid negative value while calculating.


Solution

  • Is there a way to tell which page number the textframe exists in?

    1. Yes, each TextFrame class has a parentPage property that returns a reference to the Page that the text frame is on.

    2. Each Page class has a name property that essentially returns a string of the page number

    Therefore, the following code snippet logs to the console the page number that the documents first text frame is on.

    var doc = app.activeDocument;
    var firstTextFramesPageNumber = doc.textFrames[0].parentPage.name;
    
    $.writeln(firstTextFramesPageNumber)
    

    I would like to know if there is any way I could understand that the next text frame is in next page so as to avoid negative value while calculating.

    For this you'll need to:

    1. Ascertain whether the text frame has an associated next text frame. You can achieve this by utilizing the nextTextFrame property of the TextFrame class. It will either:

      • Return null if there is no associated next text frame.
      • Or, a reference to the next text frame when there is an associated next text frame.
    2. Once you know there is an associated next text frame, you can check parentPage.name for the referenced next text frame to get its page number.

    3. To check whether the text frame and it's associated next text frame are on the same page check using the === equality operator.


    Demo Gist

    Below is a somewhat contrived example gist. It loops through all text frames in a document and logs to the console the following:

    • The page number that the current text frame is on.
    • Whether the current text frame has an associated next text frame.
    • When the current text does have an associated next text frame it tells you which page number it is on.
    #target indesign
    
    var doc = app.activeDocument;
    
    var textFrames = doc.textFrames;
    
    for (var i = 0, max = textFrames.length; i < max; i++) {
       var currentTextFrame = textFrames[i];
    
       // 1. Get the current text frames page number
       var currentTextFramePageNumber = currentTextFrame.parentPage.name
    
       $.writeln('The current text frame is on page ' + currentTextFramePageNumber)
    
       // 2. Get the current text frames associated next text frame.
       var hasNextTextFrame = currentTextFrame.nextTextFrame;
    
    
       if (hasNextTextFrame) {     
         // 3. Let's get the page number of the associated next text frame?
         var nextTextFramePageNumber = currentTextFrame.nextTextFrame.parentPage.name
    
         // 4. Is the associated next text frame on the same page number as the current text frame?
         if (currentTextFramePageNumber === nextTextFramePageNumber) {
          $.writeln('This text frame DOES have a next text frame. It\'s also on on page '
              + nextTextFramePageNumber)
         } else {
          $.writeln('This text frame DOES have a next text frame. However it\'s on a different page, it\'s on page '
              + nextTextFramePageNumber) 
         }
    
       } else {
         $.writeln('This text frame DOES NOT have a next text frame.')
       }
    
       $.writeln('--------------')
    }