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: Jeremy Reichman <email@hidden>
- Date: Thu, 08 Jan 2009 10:03:53 -0500
- Thread-topic: count pages in PDF on OS 10.4
This Python 2.5 sequence (shown as run interactively from the Terminal on
Leopard -- not as a script) works for me using a file from an AFP-mounted
volume. It uses the Quartz2D bindings for Python, which are part of the
Python install on Mac OS X (since 10.3?).
$ python
>>> import os
>>> from CoreGraphics import *
>>> pdf_filename = '/Volumes/path/to/PDF/document.pdf'
>>> provider = CGDataProviderCreateWithFilename(pdf_filename)
>>> pdf = CGPDFDocumentCreateWithProvider(provider)
>>> pages = pdf.getNumberOfPages()
>>> pages
9
>>> print(pages)
9
Substitute your own path for the pdf_filename variable's contents, and you
can try it yourself. (Above, $ is the shell prompt, and >>> is the Python
interpreter's command prompt, so don't copy/paste those.)
Since I was doing this in Terminal, I could also type "pdf_filename = '"
then drag and drop a file from the Finder into Terminal window to insert its
path, complete the quoting, and pressed Return. (Saves some time and
potential for mistyping.)
You could wrap this sequence into one longish command line and run it from
AppleScript with "do shell script." Or you could save it as a Python script
file and again run it with "do shell script." Or, you could skip AppleScript
and just use Python.*
This is derived from "Example 2: Splitting a PDF File" at:
<http://developer.apple.com/graphicsimaging/pythonandquartz.html>
That example also shows how you could specify a PDF file's path at the
command line (with sys.argv), rather than hardcoding it as I did for my
example.
There may be a shorter way to do this since I'm no expert on Quartz2D. (I
just appreciate that the bindings are there for a scripting language.) I
honestly don't know what's happening when the provider and pdf objects are
being set, but I really don't need to for this.
HTH.
--
Jeremy Reichman
* I will leave it as an exercise to the reader to deduce were my own
preferences lie.
_______________________________________________
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