Wednesday, September 22, 2021

#1 2021-09-15 09:18:51 am

Vijay_Yukthi
Member
Registered: 2021-04-16
Posts: 26

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...

Applescript:

set myFileSize to size of aFile

Last edited by Vijay_Yukthi (2021-09-15 09:19:25 am)

Offline

 

#2 2021-09-15 12:26:05 pm

Fredrik71
Member
Registered: 2019-10-23
Posts: 880

Re: Using apple script to get the PDF page width and height

You could use AObjC to get the pageSize from a page, in this example its page 1.

Applescript:

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

property theDpi : 72

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
-- get page 1
set thePage to pdfDoc's pageAtIndex:0
set pageSize to thePage's boundsForBox:(current application's kPDFDisplayBoxMediaBox)

set theSize to {pageWidth:current application's NSWidth(pageSize), pageHeight:current application's NSHeight(pageSize)}

-- set your dpi
set pixelWidth to ((theSize's pageWidth) * theDpi / 72) div 1
set pixelHeight to ((theSize's pageHeight) * theDpi / 72) div 1

return {pixelWidth, pixelHeight}


if you are the expert, who will you call if its not your imagination.

Offline

 

#3 2021-09-15 12:56:06 pm

Vijay_Yukthi
Member
Registered: 2021-04-16
Posts: 26

Re: Using apple script to get the PDF page width and height

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.

Last edited by Vijay_Yukthi (2021-09-15 01:00:04 pm)

Offline

 

#4 2021-09-15 01:21:28 pm

Fredrik71
Member
Registered: 2019-10-23
Posts: 880

Re: Using apple script to get the PDF page width and height

It use pixel value not mm


if you are the expert, who will you call if its not your imagination.

Offline

 

#5 2021-09-15 03:00:10 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1043

Re: Using apple script to get the PDF page width and height

Vijay_Yukthi wrote:

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 think Fredrik71's script works fine but needs an additional step:

Applescript:

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:

Applescript:

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.

Last edited by peavine (2021-09-15 07:26:20 pm)


2018 Mac mini - macOS Catalina - Script Debugger 8

Offline

 

#6 2021-09-16 02:03:37 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 880

Re: Using apple script to get the PDF page width and height

i only test this code against A4 format that we use in Europe.

Applescript:

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

property theDpi : 72

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
set pageSize to thePage's boundsForBox:(current application's kPDFDisplayBoxMediaBox)

set theSize to {pageWidth:current application's NSWidth(pageSize), pageHeight:current application's NSHeight(pageSize)}

-- In pixel
set pixelWidth to ((theSize's pageWidth) * theDpi / 72) as integer
set pixelHeight to ((theSize's pageHeight) * theDpi / 72) as integer


-- In millimeter
set millimeterWidth to ((theSize's pageWidth) * (25.4 / theDpi)) as integer
set millimeterHeight to ((theSize's pageHeight) * (25.4 / theDpi)) as integer

return {pixelWidth:pixelWidth, pixelHeight:pixelHeight, millimeterWidth:millimeterWidth, millimeterHeight:millimeterHeight}

Last edited by Fredrik71 (2021-09-16 10:00:56 am)


if you are the expert, who will you call if its not your imagination.

Offline

 

#7 2021-09-16 03:18:33 am

Vijay_Yukthi
Member
Registered: 2021-04-16
Posts: 26

Re: Using apple script to get the PDF page width and height

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

Last edited by Vijay_Yukthi (2021-09-16 03:32:44 am)

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)