Download a File through Safari with AppleScript
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 edito开发者_开发技巧r 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
Downloads
folder? - 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.)
tell application "Safari"
do JavaScript " alert('AppleScript successfully executed.');" in document 1
end tell
2. Open the Safari download manager through GUI scripting and paste the URL.
(Make sure to enable assistive devices.)
tell application "Finder" to set the clipboard to "http://www.google.nl/favicon.ico"
tell application "System Events"
tell application "Safari" to activate
keystroke "l" using {command down, option down}
keystroke "v" using command down
end tell
AppleScript cannot specify where to download specific files,
it can however change the default downlaod location:
(Make sure the path exists and relaunch Safari.)
do shell script "defaults write com.apple.safari DownloadsPath -string \"/Users/Anne/Desktop\""
AppleScript can indeed disable the "Open safe files after downloading" feature:
(Make sure to relaunch Safari.)
do shell script "defaults write com.apple.Safari AutoOpenSafeDownloads -boolean NO"
Safari now automatically downloads PDF files.
Unfortunately images are still being displayed.
Conclusion
AppleScript itself simply cannot achieve your goals.
I would check out this page. The example(s) provided there download all the PDF files on a page; you can adapt the scripts to suit your needs...
I happened to have a similar problem: I needed to download a lot of PDF files, and it had to be through a browser due to a login cookie. This page helped get me on the right track. Here's a batch download script using Anne's download manager paste method. It uses the free satimage osax (http://www.satimage.fr/software/en/downloads/downloads_companion_osaxen.html) for searching with regular expressions and sorting.
Note that this will attempt to download all the matching files on the page at once. From my limited testing, Safari seems to download 5 at a time and queue the rest. It may hang if there are too many files however- it hung when trying to paste a 3000 line list. I'm not sure of the effective limit.
Note also this will not download files if the page doesn't link to them by with their filenames (e.g. by file id), which is especially common in forums.
## This script batch downloads all matching HREF links from the front Safari window.
## This script requires the Satimage scripting addition. Download it at http://www.satimage.fr/software/en/downloads/downloads_companion_osaxen.html
## Access for assitive devices must be enabled in the Accessibility (or Universal Access) control pane to function.
tell application "Safari"
-- Settings
set extension to {"pdf", "jpg", "png"} --set file extensions to download
set preserve_clipboard to false -- This script sets the clipboard to a list of files to download. Set this varible to true to restore the current clipboard when it's finished.
-- End Settings
set safariversion to version
set baseurl to find text "^(http(s)?://.*?)/" in (get URL of document 1) using "\\1" with regexp and string result --get base (root) URL e.g. http://www.example.com
set relativebaseurl to find text "^(http(s)?://.*)/" in (get URL of document 1) with regexp and string result --get base for relative URLs (current directory) e.g. http://www.example.com/exa/mple/
set cursource to get source of document 1 --grab source of current page
set linklist to ""
repeat with i from 1 to number of items of extension --check for links (href) for each extension.
set searchresult to find text ("<a href=\"([^<>]*?\\." & item i of extension & ")") as string in cursource using "\\1" with all occurrences, regexp and string result --find all links matching file extension
repeat with i from 1 to number of items in searchresult --prefix links with base URL if relative links
if item i of searchresult does not start with "http" then --if a relative link
if item i of searchresult begins with "/" then --if from root
set item i of searchresult to (baseurl & item i of searchresult) as string
else --if from current directory (Safari should automatically correct "../" type links, tested in version 6.0.5)
set item i of searchresult to (relativebaseurl & item i of searchresult) as string
end if
end if
end repeat
set searchresult to sortlist searchresult with remove duplicates --remove duplicate entries from list.
set AppleScript's text item delimiters to return
if searchresult is not {} then set linklist to linklist & every item of searchresult & return as string
end repeat
if linklist is not "" then
if preserve_clipboard is true then set original to the clipboard
set the clipboard to linklist
--use UI scripting to open download window and paste list to download. Access for assitive devices must be enabled in the Accessibility control pane to function.
tell application "System Events"
tell application "Safari" to activate
tell process "Safari"
if safariversion ≥ 6 then
if not (exists pop over 1 of button 3 of tool bar 1 of window 1) then keystroke "l" using {command down, option down} --if downloads pop over isn't active, then open it. (Safari 6)
else
if not (exists window "Downloads") then --open downloads window if it's closed. (Safari 5 or below)
keystroke "l" using {command down, option down}
else --close then open downloads window to be sure it's in front and focused.
keystroke "l" using {command down, option down}
keystroke "l" using {command down, option down}
end if
end if
delay 0.5 --short delay to allow download window/pop over to open before pasting list.
keystroke "v" using command down --paste list
if preserve_clipboard is true then set the clipboard to original
end tell
end tell
end if
end tell
I saved it as an application and used BetterTouchTool (from http://www.boastr.net) to assign it to a hotkey in Safari for easy batch downloading.
精彩评论