Want to determine the number of pages in a PDF? Here are a couple of solutions.
First, you can use the “mdls” command-line tool.
choose file with prompt "Count the number of pages in this PDF file:" without invisibles
do shell script "/usr/bin/mdls -name kMDItemNumberOfPages " & quoted form of POSIX path of result & " | /usr/bin/grep -o '[0-9]\\+$'"
This method is much faster on larger files than the next one, but it requires OS X 10.4 or later.
If you can’t use the first script, then you can try something like this:
choose file with prompt "Count the number of pages in this PDF file:" without invisibles
PDFPageCount(result)
on PDFPageCount(AFile)
set FileData to read AFile
set OldDelim to AppleScript's text item delimiters
set AppleScript's text item delimiters to "/Page/"
set PageCount to (count (every text item of FileData)) - 1
if PageCount < 1 then
set AppleScript's text item delimiters to "/Page
/"
set PageCount to (count (every text item of FileData)) - 1
end if
if PageCount < 1 then
set AppleScript's text item delimiters to "/Page /"
set PageCount to (count (every text item of FileData)) - 1
end if
set AppleScript's text item delimiters to OldDelim
return PageCount
end PDFPageCount
This was originally posted by Jerome: Page count of a pdf
For what it’s worth, this second method only works on some of my PDFs (the rest of them return 0).