// GetNumberOfPages-PlacedPDF_SELECTION.jsx
// Uwe Laubender
// Slow and naive approach!
/**
* @@@BUILDINFO@@@ GetNumberOfPages-PlacedPDF_SELECTION.jsx !Version! Sun Oct 16 2016 10:07:36 GMT+0200
*/
// Select the graphic frame, that holds the placed PDF and run the snippet:
var graphicFrame = app.selection[0];
var pdfFile = File(app.selection[0].graphics[0].itemLink.filePath);
var startPageNumberTest = graphicFrame.graphics[0].pdfAttributes.pageNumber;
var doc = app.documents[0];
// Change that number to your best guess.
// The already used page number plus that range will be tested.
var testRange = 10;
// We begin testing with the next page number of the placed PDF:
// startPageNumberTest + 1
// The number of iterations in the for loop is the testRange
for(var n=startPageNumberTest+1;n<=startPageNumberTest+testRange;n++)
{
// We test placing the PDF in a temporarily added rectangle:
var tempFrame = doc.rectangles.add({geometricBounds : [0,0,100,100]});
// Here we feed the page number to the pdfPlacePreferences:
app.pdfPlacePreferences.pageNumber = n;
// Now we place the PDF where the place options will not be invoked (second argument of the method place()):
tempFrame.place(pdfFile,false,undefined);
// Let's see what happened after placing.
// What is the page number of the placed PDF?
var currentPageNumber = tempFrame.getElements()[0].graphics[0].pdfAttributes.pageNumber;
tempFrame.remove();
// InDesign will not give an error, if the value of pageNumber with pdfPlacePreferences exceeds
// the number of pages in the PDF. Instead page 1 will be placed.
// Note: We never place page 1 of the PDF.
// So if the value of the current pageNumber is 1, we reached the end of the test.
if(currentPageNumber == 1)
{
$.writeln( "Number of pages in PDF:"+"\t"+(n-1) );
break;
};
// If that will never happen, we have to expand our test range.
// Or revert to a different approach with a Divide-and-Conquer algorithm (not shown here)
if(n==startPageNumberTest+testRange)
{
$.writeln( "End of test range reached. Last tested page number:"+"\t"+(n) );
};
};