【问题标题】:Download a File through Safari with AppleScript使用 AppleScript 通过 Safari 下载文件
【发布时间】:2011-09-01 22:44:26
【问题描述】:

由于我不会真正进入的原因,我需要在 AppleScript 中编写一些内容,它将专门通过 Safari 下载一些文件。 (只是有人会双击运行它会打开 Safari 并向他们显示一个网页,同时开始一些下载。)我可以使用 AppleScript 设置文档的 URL,但这不会下载文件。这是 Safari 认为它可以理解的文件,所以它只是尝试直接打开它。我需要将文件下载到文件系统。

我在 Google 上找到的所有内容都提到了一种叫做“URL 访问脚本”的东西,但是当我使用它时,AppleScript 编辑器会要求我选择它是什么应用程序,而我似乎没有(或者不知道它在哪里) )。其他建议是调用命令行工具来下载文件,但这里的问题显然是用户在 Safari 中有一些 cookie 授权它们访问服务器资源,因此命令行工具只会出错。

所以我猜这个问题可以分解为:

  1. 如何tell application "Safari" 下载文件?
  2. 我可以指定保存文件的位置吗?还是只能去Downloads文件夹?
  3. 或者,我是否可以将 Safari 配置为不尝试打开某些文件类型,以便在显示页面之前循环浏览文档中的文件 URL?

【问题讨论】:

    标签: safari download applescript


    【解决方案1】:

    AppleScript 本身无法强制 Safari 下载文件。
    目前我可以想象两个“hacky”替代方案:

    1. 使用 JavaScript 启动下载。 (Google 获取实际脚本。)

    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 本身根本无法实现您的目标。

    【讨论】:

    • 嗯,我喜欢选项 2,但现在我遇到了一个问题,显然 Lion 中的 Safari 摆脱了下载窗口。因此,自动化首次下载被证明具有挑战性。
    • Lion 中确实缺少 Safari 下载管理器。我目前不知道如何解决这个问题。您可以考虑使用其他仍然使用经典下载管理器的浏览器。
    • 我可能不得不走那条路。不过,您的 JavaScript 想法可能仍然对我有用。我在apple.stackexchange.com 上有一个未解决的问题,关于将 Safari 设置为不以本机方式打开某些文件类型,而是始终下载它们。感谢您的帮助!
    • Safari 13 似乎损坏了defaults write com.apple.safari DownloadsPath。知道新的偏好可能是什么吗?
    【解决方案2】:

    我会查看this page。那里提供的示例下载页面上的所有 PDF 文件;您可以调整脚本以满足您的需求...

    【讨论】:

    • 链接脚本使用 Python 下载文件。然而,由于 cookie 问题,David 要求通过 Safari 下载文件。
    • 下载Safari PDF Grabber 脚本并检查其内容。 download.py 脚本负责下载文件。来自 MacScripter 的附加引用:“PDF 链接然后由位于 AppleScript 包内的 Python 脚本处理,该脚本使用 urllib 将 PDF 文档下载到下载文件夹。”
    • @Anne 哎呀!我没有看到。对不起! :(
    【解决方案3】:

    我碰巧有一个类似的问题:我需要下载很多 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 中的热键以便于批量下载。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-18
      • 2015-12-21
      • 1970-01-01
      • 2015-06-13
      • 2012-10-04
      • 2016-09-04
      相关资源
      最近更新 更多