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