Save as PDF from print dialog...

These scripts mitigate some of the pain of saving to PDF from OS X’s print dialog. They will save the front window of (some) apps to PDF using the standard print dialog (including TextEdit and Safari). They were cobbled together from examples found here and the AS Mailing List.

They work (here) as run from FastScripts menu or from a key command. They won’t work directly from Script Editor.

I haven’t found a similar script that can click the ‘PDF’ button directly (without use of the click x,y routine which was stolen outright). If that method could be found, other apps might be scripted for the same use.

The first allows you to rename the file in advance, then finishes the save process automagically. The second uses the name of the front window for the file name… but in TextEdit, may prompt you for an extension change at the end.

They’re a bit slow, and II expect they’ll both need tweaking to work properly on other machines… suggestions for refinements to the code are welcome.

Hope someone gets some use from them.

Peter B.



try
	
	tell application "System Events"
		
		set target_process to first application process whose frontmost is true and visible is true
		
		activate
		display dialog "Enter A Name For The PDF File..." & return default answer "" buttons {"Cancel", "OK"} default button 2 with title "Name The File"
		set PDF_name to text returned of the result
		
		delay 0.2
		
		tell target_process
			activate
			set frontmost to true
			
			keystroke "p" using command down
			
			set PDF_button to UI element 6 of UI element 4 of sheet 1 of window 1 of target_process
			
			set {ButtonLeft, ButtonTop} to position of PDF_button
			set {ButtonWidth, ButtonHeight} to size of PDF_button
			set clickX to ButtonLeft + (ButtonWidth div 2)
			set clickY to ButtonTop + (ButtonHeight div 2)
			
			click at {clickX, clickY}
			
			delay 1
			
			keystroke (ASCII character 31)
			keystroke (return)
			
			delay 1
			
			keystroke PDF_name
			delay 1.2
			keystroke (return)
			
		end tell
	end tell
	
end try




try
	
	tell application "System Events"
		
		set target_process to 1st application process whose frontmost is true and visible is true
		
		delay 1
		
		tell target_process
			
			keystroke "p" using command down
			
			set PDF_button to UI element 6 of UI element 4 of sheet 1 of window 1 of target_process
			
			set {ButtonLeft, ButtonTop} to position of PDF_button
			set {ButtonWidth, ButtonHeight} to size of PDF_button
			set clickX to ButtonLeft + (ButtonWidth div 2)
			set clickY to ButtonTop + (ButtonHeight div 2)
			
			click at {clickX, clickY}
			
			delay 1
			
			keystroke (ASCII character 31)
			keystroke return
			
			delay 1.2
			
			keystroke (return)
			--delay 1
			--keystroke (return)
			
		end tell
	end tell
	
end try



This script works great, as well as the improved script Nigel Garvey shared in this post: http://bbs.applescript.net/viewtopic.php?pid=73829#p73829

Anyway, and although this is an AppleScript forum, here is an alternative that may work much faster when you have a large number of files to process, which was my case. My solution is a short Objective C program that uses the CoreImage API. It’s documented here: http://jcandkimmita.info/jc/?p=61

Hopefully it helps someone.

Cheers,

Juan C.

Peter’s first script works wonderfully for me & will get lots of use. Thanks.

However, I frequently need to save as a PDF one page out of an Appleworks database. This requires me to go to the Appleworks option in the print dialogue to select the “Print: Current Record” button.

Anybody know of a way to add that function to a script?

Thomas S. England
Decatur GA 30030

Portfolio:
http://englandphoto.com/portfolio/
Images from 2006:
http://englandphoto.com/2006/

Model: intel iMac
Browser: Safari 419.3
Operating System: Mac OS X (10.4)