由于我不会真正进入的原因,我需要在AppleScript中编写一些内容,它将通过Safari专门下载一些文件。 (只需要有人双击即可运行,这将打开Safari并在启动一些下载时向他们显示网页。)我可以使用AppleScript设置文档的URL,但不会下载文件。这是Safari认为它理解的文件,所以它只是试图直接打开它。我需要将文件下载到文件系统。
我在Google上找到的所有内容都提到了一个名为“URL访问脚本”的内容,但是当我使用它时,AppleScript编辑器要求我选择它是什么应用程序,我似乎没有(或者不知道它在哪里)。其他建议是调用命令行工具来下载文件,但这里的问题显然是用户在Safari中有一些cookie授权他们使用服务器资源,因此命令行工具只会出错。
所以我想这个问题可以分解为:
tell application "Safari"
下载文件?Downloads
文件夹?答案 0 :(得分:3)
AppleScript本身无法强制Safari下载文件 目前我可以想象两个“hacky”替代方案:
1。使用JavaScript启动下载。 (谷歌的实际脚本。)
tell application "Safari"
do JavaScript " alert('AppleScript successfully executed.');" in document 1
end tell
2。通过GUI脚本打开Safari下载管理器并粘贴URL (确保启用辅助设备。)
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无法指定下载特定文件的位置,
但是它可以改变默认的下载位置:
(确保路径存在并重新启动Safari。)
do shell script "defaults write com.apple.safari DownloadsPath -string \"/Users/Anne/Desktop\""
AppleScript确实可以禁用“下载后打开安全文件”功能:
(确保重新启动Safari。)
do shell script "defaults write com.apple.Safari AutoOpenSafeDownloads -boolean NO"
Safari现在可以自动下载PDF文件 不幸的是,图像仍在显示。
<强>结论强>
AppleScript本身根本无法实现您的目标。
答案 1 :(得分:2)
我会查看this page。提供的示例下载页面上的所有PDF文件;你可以调整脚本以满足你的需求......
答案 2 :(得分:1)
我碰巧遇到了类似的问题:我需要下载大量PDF文件,并且由于登录cookie而必须通过浏览器。这个页面帮助我走上正轨。 这是使用Anne的下载管理器粘贴方法的批量下载脚本。它使用免费的satimage osax(http://www.satimage.fr/software/en/downloads/downloads_companion_osaxen.html)来搜索正则表达式和排序。
请注意,这会尝试一次下载页面上的所有匹配文件。从我的有限测试中,Safari似乎一次下载5个并排队其余部分。如果文件太多,它可能会挂起 - 但是在尝试粘贴3000行列表时它会挂起。我不确定有效限制。
另请注意,如果页面没有通过文件名(例如通过文件ID)链接到文件,则不会下载文件,这在论坛中尤为常见。
## 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
我将其保存为应用程序并使用BetterTouchTool(来自http://www.boastr.net)将其分配给Safari中的热键以便于批量下载。