Page count of a pdf

Does anyone know how to get the page count of a pdf, preferably without opening it in an application?

Problem solved. Thanks to those who looked and contemplated the delema.

Jerome it would be nice to post your solution for the record…
SC

There was another thread with this question posted that I found after I posed this question. The solution here did not prove reliable for the pdf’s that I tested it with. The solution that I came up with to eleminate Acrobat from the equation was to load the pdf file into a variable and parse it with text item delimiter’s set to the text for the page breaks of the pdf file. I found 3 different variations in the pdf’s that I tested, so I have some if–>then statements to test for these. Then you need to subtract 1 from the result to clear out the pdf header. The following handler works for all of the pdf’s I have tested:


on PDFPageCount(AFile)
	set FileData to read alias 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

If you run into any problems remember that like an post script file a pdf is basically a text file, open it in BBEdit and do a search for “Page”, there may be combination that I have not run into. Also, the second page break in the handler above does include a in it. From my testing “/Page” & return & “/” did not work, the return had to be included in the text.


set AFile to (path to desktop) & " The TGG Blue Guide.pdf"
PDFPageCount(AFile)

I’ve added this to the start of your script to test what result I get. Can you help me to understand why I might be getting the follwing error:

[Edit] I’ve read your post too quickly, – it’s morning –

Hi Tortle2,

You must pass a path to the handler not an alias and a text string ← correction


set AFile to (path to desktop as Unicode text) & " The TGG Blue Guide.pdf"

If you’re on OS X 10.4 you could use Bruce Phillips’s script found on this topic http://bbs.applescript.net/viewtopic.php?id=16162 . Read the #2 index. In your case that would be

set AFile to (path to desktop as Unicode text) & " The TGG Blue Guide.pdf"
do shell script "/usr/bin/mdls -name kMDItemNumberOfPages " & quoted form of POSIX path of Afile & " | /usr/bin/grep -o '[0-9]\\+$'"

Yannis

Hi Tortle2,
aFile needs to be set to unicode text (or string) before appended string will be coerced:

set aFile to (path to desktop as Unicode text) & "The TGG Blue Guide.pdf" as alias

Thanks gang.