Re: count pages in PDF on OS 10.4
Re: count pages in PDF on OS 10.4
- Subject: Re: count pages in PDF on OS 10.4
- From: Deivy Marck Petrescu <email@hidden>
- Date: Fri, 9 Jan 2009 08:13:37 -0500
On Jan 9, 2009, at 5:15 AM, Håvard Graudo wrote: Den 8. jan.. 2009 kl. 13.24 skrev Barry Wainwright: Not sure how reliable it is, but this seems to work on all PDFs I have tried it on:
on open theFiles
set fileData to read (item 1 of theFiles)
set {oldTIDS, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {"/Type /Pages"}}
set dataChunk to last text item of fileData
set AppleScript's text item delimiters to {"/Count "}
set pageCount to first word of last text item of dataChunk
set AppleScript's text item delimiters to oldTIDS
return pageCount
end open
Barry
Den 8. jan.. 2009 kl. 20.30 skrev Roger Howard: [...]
rxcountpages = re.compile(r"$\s*/Type\s*/Page[/\s]", re.MULTILINE|re.DOTALL)
[...]
The above approach fails on some PDFs. There are more than one instance of "/Type\s*/Page" in some PDFs and they pick the wrong one in some PDFs, returning the wrong number of pages. Using Python with getNumberOfPages() from CoreGraphics as Jeremy suggested seem to handle this correctly. Håvard
Hello Håvard,
Barry's approach is actually sound, I've checked a bunch of pdf files and they all seemed to have the count of pages in the last "\Count". So with a bit of change here is what I've got and it seems to work correctly in all kinds of pdfs I tried.
<script> set a to (choose file with prompt "Choose one file" of type "com.adobe.pdf" without invisibles and multiple selections allowed) set b to read a tid("/Count") set c to "There are " & word 1 of text item -1 of b & " pages in the document " & a tid("") return c
on tid(x) set AppleScript's text item delimiters to x end tid
</script>
Thanks Barry!
|
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden