질문
UXP scripting: How do you REALLY get the page on which a selection resides?
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);
}
