Hello everyone, I’m very new to applescript but have been learning the past few weeks but I am stuck at saving a .pdf to a directory.
I have tried a couple of methods by invoking the print dialog and then using tabs to save the file but was never able to modify the applescript with a path to save to a directory
I have also tried without the print dialog but I don’t know what code to add to specify the path and would appreciate any help, this is what I have now
tell application "Safari"
print the front document with properties {target printer:"Adobe PDF 9.0", copies:1, error handling:detailed} without print dialog
Basically I am visiting lists of websites and would like to “Save to .pdf” and need to save to a folder and not the desktop, I don’t mind if I need the print dialog pop-up but would prefer it doesn’t pop-up if possible.
I also don’t mind if something pops up and asks for the folder to “save to” but if I can just code it in my applescript then I am ok with that.
Can anybody at least confirm if this is possible? I have searched high and low for info on how to change the save location to something other than the desktop and I can’t find anything and one would think this would be something common.
If this is only intended for your own use, there is probably a clunky workaround available… but it would involve something that relies heavily on GUI scripting, the appearance of both print and save dialogs, the addition of a target folder to your Finder sidebar, etc., etc. It can almost certainly be done, but it would be ugly… and not something you’d ever want to distribute to others.
The following script dumps the PDF on the desktop, and offers no provision for renaming the resultant file in process. It works on 10.4.11 for me, but may need tweaking to get it to work elsewhere. It works for me with Safari, TextEdit and Preview, but again, your mileage may vary.
In present form, I believe the script must be run from a script menu (or keyboard shortcut) in order to work properly.
If this looks like it would be acceptable (in basis), a different folder could likely be used as the destination… again using some sort of kludge workaround.
Peter B.
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.4
keystroke "d" using command down
delay 0.2
keystroke (return)
end tell
end tell
on error
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 1 of UI element 3 of UI element 1 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.4
keystroke "d" using command down
delay 0.2
keystroke (return)
end tell
end tell
end try
end try
I’m curious also in your code after the “on error” it seems the UI Elements are slightly different? Is this perhaps to accommodate two separate print dialogs and if one fails it tries the next?
Yes… not all print dialogs are uniform… and the ‘on error’ allows for a variation among the three apps I use it for.
The script is adaptable to other apps’ print dialogs in similar fashion, but my own need didn’t extend that far… in fact I seldom use the script myself.