Determining Size of Imported PDF

I have to say, I am amazed there is still a community support this. I wish Apple hadn’t eliminated Applescript. That being said…

I would like to extend my automation a bit. I make proofs for our customers to approve by importing PDF files from our order entry system. I have some proofs that the “proofed sheet” is 17" wide and ones that the sheet is 15" wide. I current import this proofed sheet to page three of my InDesign file. How can I tell what size the PDF page is that is imported?

By knowing this, I can them determine how to crop that image and present just a single unit on that page.

David

Hi:

I may not understand exactly what you’re trying to do, but can’t you just use the built-in menu command: Object Menu/Fitting/Fit Contents to Frame Proportionally (Cmd-Opt-Shift-E)? Then the image will be as large as possible while maintaining proportions. The operation can also be scripted.

k

It appears that the OP wants to get the PDF page size by scripting the InDesign app but I’m not certain of that. So, FWIW, I’ve included below a script which returns the PDF page size without the use of InDesgin. This script was posted in a very-slightly different form by Fredrik71 in:

https://macscripter.net/viewtopic.php?id=48663

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

set theFile to (choose file)

set {pageWidth, pageHeight} to getPageSize(theFile)

on getPageSize(theFile)
	set theURL to current application's |NSURL|'s fileURLWithPath:(POSIX path of theFile)
	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
	set pageHeight to (current application's NSHeight(pageSize)) / 72
	return {pageWidth, pageHeight}
end getPageSize

here is a link for a sample PDF

https://www.dropbox.com/s/nck2la0dgtwoeuf/506677proof.pdf?dl=0

So I have template file that I open… it’s a three page InDesign file. The script that I currently have will:

open the template
import a single page PDF on page one (the art proof page)
import a single page PDF on page two (the barcode proof page)
import a single page PDF on page three (a letter size, full sheet of multiple barcodes)
then I copy the pdf on page 3 to page 2, rotate it 180 degrees, and crop it down to just a single barcode unit for proofing, in the case of the sample above, it’s a cell of two barcodes for the keytags.

my script works well for this specific form factor (2up keytags).
I would like to extend my script to copy and crop page three for a different form factor, but I would need to know that page 3 is 8.5" wide (the keytag proof) or that page 3 is 7.5" wide (the card) proof. I could extend this capabilities to more form factors based on that size of the PDF imported on page 3.

my goal is to automate this, not to manually use existing menu items.

hope this helps in the explanation
David

OK… so the next step… I can add this information to my current script, but not being familiar with using the routines,

on getPageSize(theFile)

I don’t know how to properly insert it into my code.

I put the USE commands at the very top… and the ON getPageSize routine after the last tell. But when I call out the getPageSize from within my code it I get an error, can’t continue. Any thoughts?

Here’s my messy uncommented code:


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

tell application "Finder"
	
	set volume_ to "G1"
	if (list disks) does not contain volume_ then
		mount volume "smb://graphics101/G1"
		--display dialog "volume is now mounted"
	else
		--display dialog "volume is already mounted"
	end if
	
end tell


tell application "Adobe InDesign CC 2019"
	activate
	set PDF crop of PDF place preferences to crop media
	set myRotateMatrix to make transformation matrix with properties {counterclockwise rotation angle:270}
	set myKTRotateMatrix to make transformation matrix with properties {counterclockwise rotation angle:180}
	
	set my_dialog_result to display dialog "Job Number?" default answer "" buttons {"Branded", "Generic", "cancel"} default button "Branded"
	
	set button_returned to the button returned of my_dialog_result
	set JobNum to the text returned of my_dialog_result
	
	
	set MyDestFolder to "G1:CentralPDFProofs:" & JobNum & ":"
	set MyArtPDF to "G1:CentralPDFProofs:" & JobNum & ":" & JobNum & "art.pdf"
	set MyBCPDF to "G1:CentralPDFProofs:" & JobNum & ":" & JobNum & "bc.pdf"
	set MySheetPDF to "G1:CentralPDFProofs:" & JobNum & ":" & JobNum & "sheet.pdf"
	
	
	-- BEGIN PDF SIZE CALC
	set theFile to MySheetPDF
	set {pageWidth, pageHeight} to getPageSize(theFile)
	-- END PDF SIZE CALC
	
	
	try
		tell application "Finder"
			set MyJobFolder to (name of 1st folder of folder "G1:Art Department 2:Work Files2:" whose name begins with JobNum) as string
			set myDestFile to "G1:Art Department 2:Work Files2:" & MyJobFolder & ":" & JobNum & "proof.indd"
		end tell
	on error
		set my_lost_folder to display dialog "Crap, I can't find that folder. Should I put this in the PDF folder?"
		tell application "Finder"
			set MyJobFolder to (name of 1st folder of folder "G1:CentralPDFProofs:" whose name begins with JobNum) as string
			set myDestFile to "G1:CentralPDFProofs:" & MyJobFolder & ":" & JobNum & "proof.indd"
		end tell
	end try
	
	if button_returned = "branded" then
		open file "macintosh HD:Users:davidklucznik:Desktop:PDF-VIDS.indt"
	else
		open file "macintosh HD:Users:davidklucznik:Desktop:PDF-GENERIC.indt"
	end if
	
	tell document 1 to set view preferences's ruler origin to page origin
	set rulerOrigin to ruler origin of view preferences
	if rulerOrigin is not page origin then set ruler origin of view preferences to page origin --(spread origin/page origin/spine origin)
	
	--set PDF crop of PDF place preferences to crop media
	
	tell page 1 of document 1
		try
			place file MyArtPDF
			set geometric bounds of first rectangle to {0.1, 0.1, 4, 8.4}
			send to back first rectangle
		on error
			display dialog "ARTWORK PDF missing"
		end try
		--end tell
	end tell
	
	
	tell page 2 of document 1
		--set myBox2 to make rectangle with properties {geometric bounds:{0, 8.5, 11, 17}, stroke weight:"0", name:"BCpdfBOX"}
		--tell myBox2
		try
			place file MyBCPDF
			set geometric bounds of first rectangle to {0, 8.5, 5, 17}
			send to back first rectangle
			
		on error
			display dialog "BC missing"
		end try
		--end tell
	end tell
	tell document 1 to set view preferences's ruler origin to page origin
	tell page 3 of document 1
		set myBox3 to make rectangle with properties {geometric bounds:{0, 8.5, 11, 17}, stroke weight:"0"}
		
		tell myBox3
			set fitting alignment of frame fitting options to center anchor
			try
				place file MySheetPDF
				
			on error
				display dialog "SHEET missing"
			end try
		end tell
		transform myBox3 in page coordinates from center anchor with matrix myRotateMatrix
		set geometric bounds of myBox3 to {0, 12.4375, 11, 20.9375}
		move myBox3 by {-3.9375, 0}
		
	end tell
	
	set MyDup to duplicate myBox3 to page 1 of document 1
	
	tell page 2 of document 1
		
		set geometric bounds of MyDup to {0.35, 0.14, 3, 2.85}
		move MyDup by {11.2, 5.375}
		--		tell MyDup
		--			set geometric bounds of MyDup to {5.67, 0.45, 8.36, 12.05}
		--			move by {2.5, -0.215}
		--		end tell
		transform MyDup in page coordinates from center anchor with matrix myKTRotateMatrix
		
		
		
		
		--	set geometric bounds of MyDup to {0, 8.5, 2.5, 11.125}
		
		--	move MyDup to {11.5, 5.5}
		--	set rotation angle of MyDup to 180
		
	end tell
	
	tell application "Adobe InDesign CC 2019"
		save document 1 to myDestFile
	end tell
	
end tell

on getPageSize(theFile)
	set theURL to current application's |NSURL|'s fileURLWithPath:(POSIX path of theFile)
	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
	set pageHeight to (current application's NSHeight(pageSize)) / 72
	return {pageWidth, pageHeight}
end getPageSize



I figured it out… kind of.

I moved all the file selection and job number and file paths to BEFORE the very first TELL.
I didn’t realize that you could just have code sitting out there not within a TELL statement.

Let’s see where I get to.

Success… this works well…

Next step…


	if pageWidth = 11.125 then
			--keytag positioning
			set geometric bounds of myBox3 to {0.35, 12.4375, 11, 20.9375}
			move myBox3 by {-3.9375, 0}
		else
			--card positioning
			set geometric bounds of myBox3 to {0.3, 12.7375, 11, 19.6375}
			move myBox3 by {-3.435, -0.1}
		end if

This is OK if only I have 2 conditions… I have many conditions… for example
a keytag sheet is 11.125 wide
a card sheet is 11.5 wide
a card and keytag with a label is 12.0 wide
a card and keytag without a label i s 12.25 wide
etc.

is there a IN THE CASE OF THIS CONDITION statement in applescript? How do I handle that many cases. I hope the answer isn’t nested IF THEN statements…

david

You guys are so helpful :slight_smile:
Making me work for my answers, but I think that’s the way you prefer it huh?

I can use:

if (this=1) then
do something
else if (this=2)
do something twice
else if (this=3)
do something thrice
end if

david

The simple answer to your question is that you should probably use an if statement. There are alternatives–an example of which is a list of lists–but that’s probably best avoided if at all possible.

Just as an aside, the getPageSize handler will return the page size to many decimal places and that may affect the operation of the if statement. There are simple methods to deal with this, such as the round command:

https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_cmds.html#//apple_ref/doc/uid/TP40000983-CH216-DontLinkElementID_793

Another approach, which may work better with your script, is Nigel’s suggestion in:

https://macscripter.net/viewtopic.php?pid=192039

As you can see from my many replies… I have everything working… much better with a wider range of layouts accommodated. I appreciate the help
david