You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Saving a PDF from Safari via Applescript

Hello. I’m an Applescript neophyte and have run into a problem I cannot seem to solve and no amount of Google searches has found an answer for me. It seems a simple thing to do, but I cannot get Safari (5.1, Lion) to save a PDF that is loaded as the result of a script. The relevant portion of the script is this


tell application "Safari"


savedocument 2 infilepdfDLNAME

end tell


In this example pdfDLNAME is a fully specified path, something like this:


Sarkany:Users:fenevad:Dropbox:Personal:Heenan_book:downloads:1776-07-18_New-York -Journal_page-3_4.pdf


Every time I try this it fails, regardless of whether the file exists or not. I get this error (allowing for changes in the variable value) in Safari:


The document “New-York, July 18” could not be saved as “1776-07-18_New-York-Journal_page-3_4.pdf”. The file doesn’t exist.


This result then causes problems for the AppleScript, which cannot procede as it should.


So the question I have is how simply to save a PDF loaded in Safari to a specified path location. I've seen some people suggest scripting the UI, but that is a pain when I have to determine the location for the download based on the script’s results.


Apologies if the answer is obvious to scripters, but it isn't obvious to me or to someone with more experience helping me.


Best regards,


Arle Lommel

Posted on Nov 6, 2011 10:18 PM

Reply
7 replies

Nov 7, 2011 3:44 AM in response to arle lommel

You could try GUI Scripting:


tell application "Safari"

activate

tell application "System Events" to tell process "Safari"

keystroke "s" usingcommand down-- save as…

delay 0.5

keystroke "d" usingcommand down-- save on desktop

keystrokereturn-- save

delay 0.5

if sheet 1 of sheet 1 of window 1 exists then

clickbutton 1 of sheet 1 of sheet 1 of window 1 -- replace

end if

end tell

end tell


(The above script requires that you enable access for assistive devices.)


Message was edited by: Pierre L.

Nov 7, 2011 5:58 AM in response to Pierre L.

That would probably be fine except that I need to save the file to a specified location based on other factors in the script and that location is not the same each time. I.e., saving to the desktop isn't really an option. I haven't seen in the GUI scripting method how to change the location to an arbitrary location. I suppose that I could invoke a shell script to move things around, but it seems like using a hammer to hit a gnat.


Is there a reason why Safari can't use the save action?

Nov 7, 2011 8:02 AM in response to arle lommel

arle lommel wrote:


That would probably be fine except that I need to save the file to a specified location based on other factors in the script and that location is not the same each time. I.e., saving to the desktop isn't really an option.


The previous script can easily be modified in order to do what you are asking for. Let's suppose for example that you want to save the PDF file to a folder named "Downloaded PDFs" in your Documents folder, under the name "A new PDF file.pdf":


set theFileName to "A new PDF file.pdf"

set theDestination to ((path to home folder) as text) & "Documents:Downloaded PDFs" as alias


tell application "Safari"

activate

tell application "System Events" to tell process "Safari"

keystroke "s" usingcommand down-- save as…

delay 0.5

keystroketheFileName

delay 0.5

keystroke "d" usingcommand down-- save on desktop

keystrokereturn-- save

delay 0.5

if sheet 1 of sheet 1 of window 1 exists then

clickbutton 1 of sheet 1 of sheet 1 of window 1 -- replace

end if

end tell

end tell


tell application "Finder" to move file theFileName of desktop to theDestination with replacing


Is there a reason why Safari can't use the save action?


Safari is only partially scriptable. It's not impossible that it can use the save action though. I just don't know how to do it.

Saving a PDF from Safari via Applescript

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.