Counting number of pages in a PDF

Hello all,

Wondering if you can count the number of pages inside a PDF.

I would like to drop a folder of PDFs onto the app, and then display a dialog with the number of pages in ALL the PDFs (inclusive).

Thanks

http://bbs.applescript.net/viewtopic.php?id=13085

I found that thread. Now I just get an error saying that it can’t make alias {alias “Macintosh HD:Users:Desktop:test.pdf”} into a file.

here’s the script I’m using

on open AFile	
	tell application "Finder"
		my PDFPageCount(AFile)
	end tell
end open


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

First, you don’t need that tell Finder block.

Second, the on open handler receives a list of aliases (this will come up again), not a single item. If this is a personal script, then you can get away with this:

on open droppedFiles    
	PDFPageCount(first item of droppedFiles)
end open

Finally, in the second piece of code I quoted, you try to get AFile as an alias, but it should already be an alias. I would try using this:

set FileData to read AFile

See also: PDF kit - tiger - count pages
Edit: PDF Page Count

Having that same problem. It’s returning ‘0’ (and I’m not on Tiger - yet). should be in the coming months. Is there a workaround for the ‘0’ problem while on Panther?

Thanks

This won’t work on panther (TIGER ONLY) but i didn’t see any where that used this to count pages of pdfs


set PDFFolder to "~/Documents/eBooks/regular_expressions/*.pdf"
do shell script "mdls -name kMDItemNumberOfPages " & PDFFolder & " | awk '/kMDItemNumberOfPages/ { pageCount += $3  } END { print pageCount}'"

I wrote the script in the original thread that was refered to. It has since been posted to code exchange by one of the Administrators. From my experience on Panther and Tiger it works for PDF’s that I have created in Adobe products, mostly distiller. The problem probably arises with “non-standard” PDF code that works, but is slightly different from the page break code that I used or those created with a newer or older PDF standard than the code that I looked at. If you open the PDF in a text editor such as BB Edit you should be able to search for “Page” and find the page breaks then adjust the 3 search strings to find them in the offending document.

Note: the second string being searched for should be “/Page /”

Based off of what I have seen PDF is a pretty forgiving format so there are probably other slight variations that are acceptable for viewing becouse Acrobat throws out the extra spaces or returns. It might be helpfull to parse out spaces and returns to begin with.

I havent worked with it much since I wrote it originally, it works fine for my InDesign image catalog script. When I get around to it I will probably update it to work with the methods in Tiger rather than updating my Panther work-a-round, but I’m not in a hurry to do that at the moment.