[IDCS] PDF Pagecount error

First of all, I have to thank all the helps that I got from this forum. I couldn’t recall from whom I got this handy script to check for the pagecount of PDF:-

set thePDF to (choose file without invisibles)
set thePageCount to do shell script "mdls -name kMDItemNumberOfPages '" & POSIX path of thePDF & "'"
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to " = "
set thePageCount to second text item of thePageCount as integer
set AppleScript's text item delimiters to oldDelims
display dialog thePageCount

However, this script works fine provided the PDF is residing on local computer. If the PDF is residing on other computer on the network, it will prompts with error.

Please help! Thanks!

What I use, again from the boards by Bruce Phillips, is:

on PDFPageCount(thefile)
	try
		set PageCount to do shell script "/usr/bin/mdls -name kMDItemNumberOfPages " & quoted form of POSIX path of thefile & " | /usr/bin/grep -o '[0-9]\\+$'"
		return PageCount
	on error
		display dialog ("Could not read PDF " & thefile as string) & "."
		return 1
	end try
end PDFPageCount
set x to choose file

set y to PDFPageCount(x)

This works for me on my home network.

In this thread there is an old solution that I had as well that uses plain old AppleScript. It is not as fast and probably not up to date with the latest PDF versions but if you can’t use the shell script it works. I have also had a few cases where the shell is not working that the AppleScript solution works.
http://macscripter.net/viewtopic.php?id=16274

Spotlight may not be turned on for network drives. You can check this under System Preferences>Spotlight>Privacy. I have it turned off as the constant indexing slows things down. Even when it is on, I find performance to be spotty.

-RL

Thank you, Jerome. I hope this will work on my network too.

Btw, could you please explain to me about this shell script:-

Also, I have tried the script that you mentioned, but it returns 0 for most of the PDFs…

Thanks again!

Hi Jerome,

I got no luck! I have tried the new script this morning but the problem of unable to obtain the pagecount of the PDF residing on the Server still persists.

Please advise! Tks!

I looked into it a bit and found out that the mdls shell command is reading the meta data from the file. It seems to work over my home network fine but that does not mean anything. If you have a UNIX server or Windows server that might be causing a problem, check with you network guru to see. There also might be a permissions issue that is not allowing the shell script to work over the network, again check with your network guru. Someone else here might have a better idea of what could cause the problem.

The other script (altered slightly) works by actually reading the PDF and parsing it based off of the page break in the PDF code. So the delimiters listed are used in versions of PDF’s that I had available when I wrote it. It looks like thing have changed a bit, and it is not perfect, I have one that is showing 60 pages based on the pure AppleScript solution but there are only 56 pages in the PDf. Opening a newer PDF it looks like there is a new page break (/Page>>) that they are using now. What I do, rather than reading through volumes of PDF specifications, is open the PDF in a text editor and search for “page” looking for a pattern. Once I find one I add that to the code and test it on a number of PDFs to see if the solution is working. As long as you are making your PDFs in a predictable way you should be able to find a solution that works.

set x to choose file with prompt "Count the number of pages in this PDF file:" without invisibles
set y to PDFPageCount(x)
set z to do shell script "/usr/bin/mdls -name kMDItemNumberOfPages " & quoted form of POSIX path of x & " | /usr/bin/grep -o '[0-9]\\+$'"

return {y, z}
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
	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

Thanks for your advice, Jerome.

  1. The server that I mentioned is Apple XServer (Tiger). I have also tried on non-server Mac, the problem is similar.

  2. The new script does not works on my network too. It promts “The command exited with a non-zero status.” while it execute this line:

set z to do shell script "/usr/bin/mdls -name kMDItemNumberOfPages " & quoted form of POSIX path of x & " | /usr/bin/grep -o '[0-9]\\+$'"
  1. Even I used it on the local HD, when come to heavy graphic-content PDF with more than 7 pages (in my case), it prompts with “Out of memory.” while reading file.

  2. Btw, my PDF was direct exported from ID CS3 at level 1.5

I hope these info are good enough for you to help diagnose my problem.

Something curious, when I open up the PDF using a text editor, I don’t see any hint to show the pagecount. May I know where is the pagecount data being residing?

Thanks!

I can’t help with the network issue, I don’t know why it would not allow the shell script to read the metadata unless it did not exist in the file or there were a permissions issue.

In a PDF there is not a line or code for page count, just page breaks. For PDF 1.5 and above it looks like that is “/Page>>”. The script below eliminates the shell script portion. I just ran it on an 18.4 MB file with 16 pages without a problem, well except the long time it takes to process the file. Maybe a shell script guru could put together a grep command to do the same thing faster.

I’m not sure if there is anything else you can do if you can’t get either option to work. What do you need the page count for?

set x to choose file with prompt "Count the number of pages in this PDF file:" without invisibles
set y to PDFPageCount(x)


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
	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

Still got no luck!

Anyway, thanks a lot for your time and help, Jerome.

Btw, do you on your firewall at your home network? Do you think it could the cause? May be I should experiment on this…