Using apple script to get the PDF page width and height

Hi all,

Is it possible to get the PDF pages width and height using applescript? I get the file size using below code but not able to get the page width. Any help is much appreciated…

set myFileSize to size of aFile

Thanks Fredrik!

I have checked the code, its working fine! Its produced width and height are in mm? Actually I need the output in mm.

I’m going to use the trim width(mm) and height(mm) to some other functionalities.

I think Fredrik71’s script works fine but needs an additional step:

use framework "Foundation"
use framework "Quartz"
use scripting additions

set thePath to POSIX path of (choose file)
set theURL to current application's |NSURL|'s fileURLWithPath:thePath
set pdfDoc to current application's PDFDocument's alloc()'s initWithURL:theURL
set thePage to pdfDoc's pageAtIndex:0 -- page 1
set pageSize to thePage's boundsForBox:(current application's kPDFDisplayBoxMediaBox)
set pageWidth to (current application's NSWidth(pageSize)) / 72 * 25.4 -- returns width in mm
set pageHeight to (current application's NSHeight(pageSize)) / 72 * 25.4 -- returns height in mm

You can get the same result using ExifTool and it returns the size of every page, which must then be parsed and converted to mm as above:

set thePDF to POSIX path of (choose file)
do shell script "/usr/local/bin/exiftool -v " & quoted form of thePDF & " |grep MediaBox"

I tested both of the above on several PDF’s of known page size (as shown by Preview) and they seem to work fine but it’s always good to double-check.

Amazing! Thanks you both. You guys are the best…:slight_smile: