Copy link to clipboard
Copied
Hi,
I want to find out on which page of a document an external XRef can be found. Is there a simple way to manage that using ExtendScript? The equivalent in FrameScript is XRef.Page.PageNumString.
Thank you in advance.
Here is the modified function, which now requires a doc object as its second parameter.
...function getPage (obj, doc) {
var frame = 0, cell = 0;
var objType = "", prop = 0;
while (obj) {
frame = obj;
objType = obj.constructor.name;
switch (objType) {
case "SubCol" :
obj = obj.ParentTextFrame;
break;
case "Tbl" :
obj = obj.FirstRowInTbl.FirstCellInRow;
break;
case "Row" :
Copy link to clipboard
Copied
Here is a general purpose function to get you started. You pass it an object and it returns the parent page object. At this point, it does not handle XRefs directly, but you can pass in the paragraph that contains the XRef.
var page = getPage(xref.TextRange.beg.obj);
should work.
Rick
function getPage (obj) {
var frame = 0, cell = 0;
var objType = "";
while (obj) {
frame = obj;
objType = obj.constructor.name;
switch (objType) {
case "SubCol" :
obj = obj.ParentTextFrame;
break;
case "Tbl" :
obj = obj.FirstRowInTbl.FirstCellInRow;
break;
case "Row" :
obj = obj.FirstCellInRow;
break;
case "Cell" :
case "Pgf" :
case "AFrame" :
obj = obj.InTextFrame;
break;
case "TextLine" :
case "TextFrame" :
case "UnanchoredFrame" :
case "Arc" :
case "Ellipse" :
case "Group" :
case "Inset" :
case "Line" :
case "Math" :
case "Polygon" :
case "Polyline" :
case "Rectangle" :
case "RoundRect" :
if (obj.FrameParent.ObjectValid()) {
obj = obj.FrameParent;
} else {
obj = 0;
}
break;
default:
// Prevent endless loop if unknown object type is found.
obj = 0;
break;
}
}
if (frame) {
return frame.PageFramePage;
} else {
return 0;
}
}
Copy link to clipboard
Copied
Hi J,
I'm not a FrameScript user, but it appears that the FrameScript folks definitely simplified this operation for you. ES follows the FDK model, which is considerably less simple. Basically, you need to:
- Find the paragraph that the xref is in
- Find the text frame that the paragraph is in
- Find the top level parent frame for the text frame; that is, the page frame
- Find the page for the page frame
- Retrieve the page number from the page
Assuming you already have the xref object as 'xref', the following code should do it:
var tr = xref.TextRange;
var pageFrame;
var tempFrame = tr.beg.obj.InTextFrame;
while(tempFrame.ObjectValid())
{
pageFrame = tempFrame;
tempFrame = tempFrame.FrameParent;
}
var page = pageFrame.PageFramePage;
var pageNum = page.PageNumString;
alert(pageNum);
Copy link to clipboard
Copied
There is a potential problem with Russ's and my code.
var pageFrame = 0;
var tr = xref.TextRange;
var pgf = tr.beg.obj;
This gets the paragraph object that contains the cross-reference. Technically, the paragraph could start on one page and the cross-reference could be on another page. Although this may be unlikely, it is probably best to account for it. Here is one way you might do it.
var doc = app.ActiveDoc;
var textLoc = xref.TextRange.beg;
var prop = doc.GetTextPropVal (textLoc, Constants.FP_InTextObj);
var textObj = prop.propVal.obj; // SubCol or Cell object.
if (textObj.constructor.name === 'SubCol') {
var textFrame = textObj.ParentTextFrame;
} else { // Cell
var textFrame = textObj.InTextFrame;
}
while(textFrame.ObjectValid())
{
pageFrame = textFrame;
textFrame = textFrame.FrameParent;
}
var page = pageFrame.PageFramePage;
var pageNum = page.PageNumString;
alert(pageNum);
When I get some time, I will update my getPage function to accomodate XRefs.
Copy link to clipboard
Copied
Here is the modified function, which now requires a doc object as its second parameter.
function getPage (obj, doc) {
var frame = 0, cell = 0;
var objType = "", prop = 0;
while (obj) {
frame = obj;
objType = obj.constructor.name;
switch (objType) {
case "SubCol" :
obj = obj.ParentTextFrame;
break;
case "Tbl" :
obj = obj.FirstRowInTbl.FirstCellInRow;
break;
case "Row" :
obj = obj.FirstCellInRow;
break;
case "Cell" :
case "Pgf" :
case "AFrame" :
obj = obj.InTextFrame;
break;
case "TextLine" :
case "TextFrame" :
case "UnanchoredFrame" :
case "Arc" :
case "Ellipse" :
case "Group" :
case "Inset" :
case "Line" :
case "Math" :
case "Polygon" :
case "Polyline" :
case "Rectangle" :
case "RoundRect" :
if (obj.FrameParent.ObjectValid()) {
obj = obj.FrameParent;
} else {
obj = 0;
}
break;
case "XRef" :
prop = doc.GetTextPropVal (obj.TextRange.beg, Constants.FP_InTextObj);
var obj = prop.propVal.obj;
break;
default:
// Prevent endless loop if unknown object type is found.
obj = 0;
break;
}
}
if (frame) {
return frame.PageFramePage;
} else {
return 0;
}
}
Copy link to clipboard
Copied
Rick, good catch. When I added the ability to match elements based on page number to FrameSLT XPath, I had to do the same thing, since an element can span multiple pages.
Russ
Copy link to clipboard
Copied
Thank you both for the replys, that really helped to solve my problem.
Jürgen
Copy link to clipboard
Copied
Hi Jürgen,
Please mark one of our answers as Correct and one as Helpful. Thank you very much.
Rick