PDF kit - tiger - count pages

Hi all,

this link: http://developer.apple.com/documentation/GraphicsImaging/Conceptual/PDFKitGuide/PDFKit_Prog_Conc/chapter_2_section_3.html

says this:
PDF Document
The primary PDF Kit utility class is PDFDocument, which represents PDF data or a PDF file. The other utility classes are either instantiated from methods in PDFDocument, as are PDFPage and PDFOutline; or support it, as do PDFSelection and PDFDestination.
You initialize a PDFDocument object with PDF data or with a URL to a PDF file. You can then ask for the page count, add or delete pages, perform a find, or parse selected content into an NSString object.

Issue: I need to count the number of pages of a pdf without opening it in a 3rd party app.
Quest: Does anyone know of a clean way to scipt pdfKit or to find out the number of pages in a pdf: ?
… tough one for me :expressionless:

That doesn’t have much to do with AppleScript; also, PDFKit isn’t something you would control via AppleScript.

However, if you have OS X v10.4, then you should be able to use this:

choose file with prompt "Count pages in this PDF file:" without invisibles
set theFile to POSIX path of result

try
	do shell script "/usr/bin/mdls -name kMDItemNumberOfPages " & quoted form of theFile & " | /usr/bin/grep -o '[0-9]\\+$'"
on error errorMsg number errorNum
	if errorNum is 1 then
		display alert "Page Count Unavailable" message "The page count for "" & theFile & "" is unavailable." buttons "OK" default button 1
	else
		display alert "Error " & errorNum message errorMsg buttons "Cancel" default button 1
	end if
end try

ok this is cocoa, I though this library could have associated applescript commands.
Anyway, Bruce this script works very well in 10.4. Thank you.

why does it only work in 10.4? what commands aren’t allowed in 10.3.9?

Thanks

“mdls” uses Spotlight.

Ahh I see. Thanks for the info.

I’m hoping to be able to choose a folder with several PDF files in it. Then get a total count of PDF pages inside all the documents (one number). So I’m getting all the files into a list, but then it’s having a POSIX path problem. Here’s how I’m getting the list:

set mystartfolder to (choose folder with prompt "Choose job folder for collection of Bobbi's Quark files...")

tell application "Finder"
	activate
	open folder mystartfolder
	
	set AllPDFList to (files of (entire contents of mystartfolder))
	
end tell

Thanks.

I’m having to get whatever I can do in TextEdit, then take it to a co-worker’s machine that has Tiger. What a hassle.

My original script, adapted to count all the PDFs in one folder (not subfolders):

choose folder with prompt "Count pages in all PDF files in this folder:" without invisibles

try
	do shell script "cd " & quoted form of POSIX path of result & ¬
		"; /usr/bin/mdls -name kMDItemNumberOfPages *.pdf" & ¬
		" | /usr/bin/grep -o '[0-9]\\+$'" & ¬
		" | /usr/bin/sed s/$/\\ +\\ /g" & ¬
		" | /usr/bin/tr -d '\\n'" & ¬
		" | /usr/bin/sed s/\\ +\\ $//" & ¬
		" | /usr/bin/bc"
on error errorMsg number errorNum
	display alert "Error " & errorNum message errorMsg buttons "Cancel" default button 1
end try

As for your problem. you don’t explain it very well, but I bet I know what it is. The Finder often returns documents objects (instead of aliases), which aren’t useful to other applications. Perhaps you want something like this:

choose folder with prompt "Choose job folder for collection of Bobbi's Quark files."
set myStartFolder to result

tell application "Finder"
	launch
	set PDFList to (files of (entire contents of myStartFolder) whose name extension is "pdf") as alias list
end tell

Getting an error on this script saying Cant Read

It works when doing a single PDF, but when I’ve tried to make it work w/ entire folder, it errors.

Here it is: (I posted the working script below also)

--CHOOSING A FOLDER OF PDFs DOES NOT WORK. SEE SCRIPT BELOW

set theFolder to choose folder with prompt "Select the folder containing the PDFs:" without invisibles
tell application "Finder"
	set pdfList to (files of (entire contents of theFolder)) --whose file type is "PDF ")
	repeat with thePDF in the pdfList
		my PDFPageCount(thePDF)
	end repeat
end tell



on PDFPageCount(AFile)
	display dialog AFile as text
	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


--CHOOSING A SINGLE PDF WORKS. SEE SCRIPT BELOW
(*     
choose file with prompt "Count the number of pages in this PDF file:" without invisibles
PDFPageCount(result)

PDFPageCount(result)

on PDFPageCount(AFile)
	display dialog AFile as text
	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
*)

can anyone help with the issue I’m having putting this in a loop?

I keep getting this error “Finder got an error: Can’t make document file “test.pdf” of folder “PDFs” of folder “countPDFTest” of folder “Desktop” of folder “myuser” of folder “Users” of startup disk into a file.”

The Finder typically returns document objects, which are not useful outside of the Finder. Tell the Finder that you want an alias list instead.

set pdfList to (files of (entire contents of theFolder)) as alias list

set theFolder to choose folder with prompt "Select the folder containing the PDFs:" without invisibles
tell application "Finder"
	set pdfList to (files of (entire contents of theFolder) whose file type is "PDF ")
	repeat with thePDF in the pdfList
		my PDFPageCount(thePDF as string)
	end repeat
end tell

on PDFPageCount(AFile)
	display dialog 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

The problem appears to be in the file description you were passing to the handler. Coercing it to a string then adding alias to the read command fixes the problem, at least on my computer.

Thanks Jerome, that seems to be working great…Until I throw it a file that is too large. I continually get an Out of Memory error.

Any suggestions?

Thanks guys.

Well, I’m not sure how to fix an out of memory error in the script. I’ve run it on some pretty large files (page size) and never run into that problem. I would imagine that you would need to break the document apart into chunks and load those in sequence. This might be problematic if you split the page break between two “load” cycles though.

Yeah, it’s odd. I have this to determine if the doc is too large to process (seems to be 32mb). Not sure how you could then break up those into smaller pieces. If you have any suggestions, that would be great. I’m not sure how to do it at all.

Thanks

set theFolder to choose folder with prompt "Select the folder containing the PDFs:" without invisibles
tell application "Finder"
	set pdfList to (files of (entire contents of theFolder) whose file type is "PDF ")
	set documentCount to count items in pdfList
	--display dialog documentCount
	if documentCount = 0 then
		set pdfList to (files of (entire contents of theFolder) whose name contains ".pdf")
		set documentCount to count items in pdfList
		--display dialog documentCount
	end if
	set theLogFile to ""
	set totalPageCount to 0
	set tooBigList to {}
	repeat with thePDF in the pdfList
		set fileName to name of thePDF
		set theFileSize to my GetFileSize(thePDF)
		if theFileSize is greater than 32 then
			set end of tooBigList to thePDF
		else
			set PageCount to my PDFPageCount(thePDF as string, fileName)
			set theLogFile to theLogFile & fileName & " contains " & (PageCount as text) & " Page(s)." & return
			set totalPageCount to totalPageCount + PageCount
		end if
	end repeat
	display dialog (documentCount as text) & " PDF Documents." & return & (totalPageCount as text) & " Total Pages."
	--display dialog theLogFile
	set theList to "The following Documents are too large. " & return & "They will have to be manually counted." & return & return
	
	repeat with thePDF in tooBigList
		set theName to the name of thePDF
		set theList to theList & theName & return
	end repeat
	display dialog theList
end tell



on PDFPageCount(AFile, fileName)
	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
	if PageCount < 1 then
		set AppleScript's text item delimiters to "/Pages"
		set PageCount to (count (every text item of FileData)) - 1
	end if
	set AppleScript's text item delimiters to OldDelim
	
	return PageCount
end PDFPageCount

on GetFileSize(thisFile)
	
	tell application "Finder"
		set fileSizediv to (((size of thisFile) / 1024)) div 1024
		set fileSizemod to ((((size of thisFile) / 1024)) mod 1024) div 100
		set fileSize to fileSizediv + fileSizemod / 10
		
	end tell
	
	--display dialog ("Div=" & fileSizediv as text) & return & ("Mod=" & fileSizemod as text) & return & ("Total=" & fileSize as text)
	return fileSize
	
	
end GetFileSize