For reasons I won’t really go into, I need to write something in AppleScript which will download some files specifically through Safari. (Just something that someone will double-click to run which will open Safari and show them a web page while initiating some downloads.) I can set the URL of the document with AppleScript, but that doesn’t download the file. It’s a file that Safari thinks it understands, so it just tries to open it directly. I need the file to be downloaded to the file system.
Everything I find on Google mentions something called “URL Access Scripting” but when I use that the AppleScript editor asks me to select what application it is, which I don’t seem to have (or don’t know where it is). Other suggestions are to call command line tools to download the file, but the issue here is apparently that the user has some cookie(s) in Safari that authorize them to the server resource, so the command-line tools will just get an error.
So I guess the question breaks down to:
- How do I
tell application "Safari"to download a file? - Can I specify where it saves the file? Or can it only go to the
Downloadsfolder? - Alternatively, can I configure Safari to not try to open certain file types so that maybe I just loop through the file URLs in the document before showing the page?
AppleScript itself cannot force Safari to download files.
Currently I can imagine two ‘hacky’ alternatives:
1. Initiate the download using JavaScript. (Google for the actual script.)
2. Open the Safari download manager through GUI scripting and paste the URL.
(Make sure to enable assistive devices.)
AppleScript cannot specify where to download specific files,
it can however change the default downlaod location:
(Make sure the path exists and relaunch Safari.)
AppleScript can indeed disable the “Open safe files after downloading” feature:
(Make sure to relaunch Safari.)
Safari now automatically downloads PDF files.
Unfortunately images are still being displayed.
Conclusion
AppleScript itself simply cannot achieve your goals.