<HTML>
<HEAD>
<TITLE>Python CoreGraphics script running in Applescript</TITLE>
</HEAD>
<BODY>
<FONT FACE="Geneva, Verdana, Helvetica, Arial"><SPAN STYLE='font-size:9pt'>I have a script that needs to get the height and width dimensions of the media box of a PDF using Applescript. In OS X 10.4, I was using the following:<BR>
<BR>
</SPAN></FONT><BLOCKQUOTE><SPAN STYLE='font-size:9pt'><FONT COLOR="#1500FB"><FONT FACE="Lucida Grande">set</FONT></FONT><FONT FACE="Helvetica, Verdana, Arial"> </FONT><FONT COLOR="#537E0B"><FONT FACE="Lucida Grande">shellScript</FONT></FONT><FONT FACE="Helvetica, Verdana, Arial"> </FONT><FONT COLOR="#1500FB"><FONT FACE="Lucida Grande">to</FONT></FONT><FONT FACE="Helvetica, Verdana, Arial"> </FONT><FONT FACE="Lucida Grande">"python "</FONT><FONT FACE="Helvetica, Verdana, Arial"> </FONT><FONT FACE="Lucida Grande">&</FONT><FONT FACE="Helvetica, Verdana, Arial"> </FONT><FONT COLOR="#1500FB"><FONT FACE="Lucida Grande">quoted form</FONT></FONT><FONT FACE="Helvetica, Verdana, Arial"> </FONT><FONT COLOR="#1500FB"><FONT FACE="Lucida Grande">of</FONT></FONT><FONT FACE="Helvetica, Verdana, Arial"> </FONT><FONT COLOR="#537E0B"><FONT FACE="Lucida Grande">PythonPath</FONT></FONT><FONT FACE="Helvetica, Verdana, Arial"> </FONT><FONT FACE="Lucida Grande">&</FONT><FONT FACE="Helvetica, Verdana, Arial"> </FONT><FONT FACE="Lucida Grande">" "</FONT><FONT FACE="Helvetica, Verdana, Arial"> </FONT><FONT FACE="Lucida Grande">&</FONT><FONT FACE="Helvetica, Verdana, Arial"> </FONT><FONT COLOR="#537E0B"><FONT FACE="Lucida Grande">POSIXThisFile<BR>
</FONT></FONT><FONT FACE="Lucida Grande"><FONT COLOR="#1500FB">set</FONT></FONT><FONT FACE="Helvetica, Verdana, Arial"> </FONT><FONT COLOR="#537E0B"><FONT FACE="Lucida Grande">dims</FONT></FONT><FONT FACE="Helvetica, Verdana, Arial"> </FONT><FONT COLOR="#1500FB"><FONT FACE="Lucida Grande">to</FONT></FONT><FONT FACE="Helvetica, Verdana, Arial"> </FONT><FONT COLOR="#1500FB"><FONT FACE="Lucida Grande">every</FONT></FONT><FONT FACE="Helvetica, Verdana, Arial"> </FONT><FONT COLOR="#1500FB"><FONT FACE="Lucida Grande">paragraph</FONT></FONT><FONT FACE="Helvetica, Verdana, Arial"> </FONT><FONT COLOR="#1500FB"><FONT FACE="Lucida Grande">of</FONT></FONT><FONT FACE="Helvetica, Verdana, Arial"> </FONT><FONT FACE="Lucida Grande">(<FONT COLOR="#1500FB">do shell script</FONT></FONT><FONT FACE="Helvetica, Verdana, Arial"> </FONT><FONT COLOR="#537E0B"><FONT FACE="Lucida Grande">shellScript</FONT></FONT><FONT FACE="Lucida Grande">)<BR>
</FONT></SPAN></BLOCKQUOTE><SPAN STYLE='font-size:9pt'><FONT FACE="Helvetica, Verdana, Arial"><BR>
<BR>
The PythonPath is a path to the below Python script and the POSIXThisFile is the path to the PDF.<BR>
<BR>
The response is two paragraphs where paragraph 1 is the width and paragraph 2 is the height.<BR>
<BR>
<BR>
Here’s the python script:<BR>
<BR>
# step 1: import the required modules<BR>
import os, sys<BR>
from CoreGraphics import *<BR>
<BR>
if len( sys.argv ) != 2:<BR>
print "ERROR: useage: python example2.py pdf_filename"<BR>
sys.exit(1)<BR>
<BR>
# step 2: read the pdf file name from the command line arguments<BR>
pdf_filename = sys.argv[1]<BR>
pdf_name, ext = os.path.splitext( pdf_filename )<BR>
<BR>
# step 3: create the input PDF document<BR>
provider = CGDataProviderCreateWithFilename( pdf_filename )<BR>
pdf = CGPDFDocumentCreateWithProvider( provider )<BR>
if pdf is None:<BR>
print "ERROR reading PDF document - \r\n check that the supplied filename points to a PDF file"<BR>
sys.exit(1)<BR>
<BR>
page_number = 1<BR>
<BR>
page_rect = pdf.getMediaBox( page_number )<BR>
page_width = int(page_rect.getWidth())<BR>
page_height = int(page_rect.getHeight())<BR>
<BR>
print "%d \r\n%d" % (page_width, page_height)<BR>
<BR>
<BR>
Under OS 10.5, this script now uses a deprecated function of CoreGraphics and I get an error that says:<BR>
<BR>
<BR>
</FONT></SPAN><BLOCKQUOTE><SPAN STYLE='font-size:9pt'><FONT FACE="Helvetica, Verdana, Arial"> <Error>: The function `CGPDFDocumentGetMediaBox' is obsolete and will be removed in an upcoming update. Unfortunately, this application, or a library it uses, is using this obsolete function, and is thereby contributing to an overall degradation of system performance. Please use `CGPDFPageGetBoxRect' instead.<BR>
</FONT></SPAN></BLOCKQUOTE><SPAN STYLE='font-size:9pt'><FONT FACE="Helvetica, Verdana, Arial"><BR>
<BR>
Can anyone steer me in the right direction on how to work with the CGPDFPageGetBoxRect function?<BR>
<BR>
Thanks.<BR>
Jim<BR>
</FONT></SPAN>
</BODY>
</HTML>