Skip to main content
Inspiring
February 10, 2026
질문

UXP scripting: How do you REALLY get the page on which a selection resides?

  • February 10, 2026
  • 3 답변들
  • 119 조회

All the examples you find tell you to traverse up through the selection’s parents until you hit the Page object. But in my experience, you never do. The traversal goes:
 

InsertionPoint → TextFrame → Spread → Document → Application
 

Now what?
 

For the curious, here’s the code:

function getPage(obj)
{
if (obj == null)
return null;

if (obj.constructor.name == "Page")
{
return obj;
}
else if(obj.constructor.name == "Application")
{
// If we reach the application, something is wrong. Prevent endless recursion.
return null;
}

// Handle cases where the item might be in a text frame
if (obj.parentTextFrames && obj.parentTextFrames.length > 0)
{
return getPage(obj.parentTextFrames[0]);
}

// Recursively go up the object model
return getPage(obj.parent);
}

 

    3 답변

    rob day
    Community Expert
    Community Expert
    February 12, 2026

    Hi ​@Thomas_Calvin , I assume you can write a while statement with UXB? That’s how I would do it with ExtendScript. This seems to work for text in nested tables, text on a path, anchored text:

     

    var sp = getTextPage(app.selection[0])
    alert("Selected Text Starts on Page " + sp)


    /**
    * Gets the starting page of the selected text
    * @ param s the selected text
    * @ return text se;lection’s page
    */
    function getTextPage(s){
    var b = false;
    var a = []
    var pn
    if (s != undefined && s.hasOwnProperty("pointSize")) {
    while (!b) {
    if (s.constructor.name == "Spread" || s.constructor.name == "Story" ) {
    b = true
    } else {
    s = s.parent
    a.push(s)
    }
    }
    if (a.length == 1) {
    if (app.selection[0].parentTextFrames[0].constructor.name == "TextPath") {
    pn = app.selection[0].parentTextFrames[0].parent.parentPage.name
    } else {
    pn = app.selection[0].parentTextFrames[0].parentPage.name
    }
    } else {
    pn = a[a.length-2].parentPage.name
    }
    return pn
    }else{
    return "Please select some text"
    }
    }

     

    Thomas_Calvin작성자
    Inspiring
    February 13, 2026

    Thanks! I did get it working satisfactorily, but then discovered that app.activeWindow.activePage seems to work reliably, and solves the problem for my use case.

    rob day
    Community Expert
    Community Expert
    February 13, 2026

    If this is for in-house use it might not matter, but the active page and the selection’s page can be different:

     

    var ap = app.activeWindow.activePage.name
    var sp = getTextPage(app.selection[0])
    alert("\nSelected Text Starts on Page " + sp + "\n Active Page is " + ap)

     

    Peter Kahrel
    Community Expert
    Community Expert
    February 12, 2026

    [Edit: I hadn’t spotted the replies after Dirk’s doomesday scenario. Have to get used to collapsed threads!]

     

    > All the examples you find tell you to traverse up through the selection’s parents until you hit the Page object.

     

    The entity that you asked this question lives in the past. The function you showed dates back to Dave Saunders, who started using it many years ago.

     

    As ​@leo.r and ​@Dirk Becker mention, you should use the parentPage property. You would first try to get a reference the the selection’s parentTextFrame, then return the text frame’s parent page. Something like

     

    myInsertionPoint.parentTextFrames[0].parentPage;

     

    You should of course take into account all scenarios that Dirk warned about (overset text, not on a page, etc). So you would tread carefully, look before you leap, etc etc, vaguely as in

    // ip is an insertion point

    if (ip.parentTextFrames.length > 0 // Not overset text
    && ip.parentTextFrames[0].parentPage != null) { // On a page
    // return the page
    } else {
    // report and/or handle the situation
    }

     

    Thomas_Calvin작성자
    Inspiring
    February 12, 2026

    Thanks. Yep, in the end I can just use app.activeWindow.activePage.

    leo.r
    Community Expert
    Community Expert
    February 11, 2026

    parentPage property of text frame

    Thomas_Calvin작성자
    Inspiring
    February 11, 2026

    Thanks for that. It might work in some circumstances, but right now (I just happen to be on a different page) the hierarchy has become:

    InsertionPoint → Story → Document → Application

    Legend
    February 11, 2026

    As you mention UXP, ask your AI to consider https://developer.adobe.com/indesign/uxp/resources/migration-guides/extendscript/ on the use of constructor.name .

    Here some edge cases for testing:

    Your selection is in overset, in the galley view / text editor window.

    It is in a text on path of an anchored object which itself is in overset.

    It is in a text frame that completely sits on the pasteboard.

    It could be that insertion point between two chained text frames. One of them is on page 10, the other on page 123-

    Your selection could be a text range, that stretches from page 1 to page 124.

    Plenty more special cases with text on path, or tables, or XML .…

    Have you ever used the pages tool, to select actual pages?

    Happy programming.