使用AppleScript通过Safari下载文件

使用AppleScript通过Safari下载文件,safari,download,applescript,Safari,Download,Applescript,出于我不想深入讨论的原因,我需要在AppleScript中编写一些东西,专门通过Safari下载一些文件。(有人会双击来运行它,它会打开Safari并在启动下载时向他们显示一个网页。)我可以用AppleScript设置文档的URL,但这不会下载文件。Safari认为它能理解这个文件,所以它只是试图直接打开它。我需要将文件下载到文件系统 我在Google上找到的所有东西都提到了一种叫做“URL访问脚本”的东西,但当我使用它时,AppleScript编辑器会让我选择它是什么应用程序,而我似乎没有(或

出于我不想深入讨论的原因,我需要在AppleScript中编写一些东西,专门通过Safari下载一些文件。(有人会双击来运行它,它会打开Safari并在启动下载时向他们显示一个网页。)我可以用AppleScript设置文档的URL,但这不会下载文件。Safari认为它能理解这个文件,所以它只是试图直接打开它。我需要将文件下载到文件系统

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

所以我想问题可以归结为:

  • 如何
    告诉应用程序“Safari”
    下载文件
  • 我可以指定它保存文件的位置吗?或者它只能进入
    下载
    文件夹
  • 或者,我可以将Safari配置为不尝试打开某些文件类型,这样我就可以在显示页面之前循环浏览文档中的文件URL吗

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

    1。使用JavaScript启动下载。(谷歌搜索实际脚本。)

    2。通过GUI脚本打开Safari下载管理器并粘贴URL。
    (确保启用辅助设备。)

    AppleScript无法指定下载特定文件的位置,
    但是,它可以更改默认的downlaod位置:
    (确保路径存在并重新启动Safari。)

    AppleScript确实可以禁用“下载后打开安全文件”功能:
    (请确保重新启动Safari。)

    Safari现在自动下载PDF文件。
    不幸的是,图像仍在显示

    结论


    AppleScript本身根本无法实现您的目标。

    我想看看。此处提供的示例下载页面上的所有PDF文件;您可以调整脚本以满足您的需要…

    我碰巧遇到了一个类似的问题:我需要下载大量PDF文件,并且由于登录cookie,必须通过浏览器进行下载。这一页帮助我找到了正确的方向。 下面是一个使用Anne的下载管理器粘贴方法的批量下载脚本。它使用免费的satimage osax()进行正则表达式搜索和排序

    请注意,这将尝试一次下载页面上所有匹配的文件。从我有限的测试来看,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
    
    ##此脚本从前面的Safari窗口批量下载所有匹配的HREF链接。
    ##此脚本需要添加Satimage脚本。下载地址http://www.satimage.fr/software/en/downloads/downloads_companion_osaxen.html
    ##必须在可访问性(或通用访问)控制窗格中启用对辅助设备的访问才能正常工作。
    告诉应用程序“Safari”
    --背景
    将扩展名设置为{“pdf”、“jpg”、“png”}——设置要下载的文件扩展名
    将preserve_clipboard设置为false——此脚本将剪贴板设置为要下载的文件列表。将此变量设置为true,以在完成时恢复当前剪贴板。
    --结束设置
    将safariversion设置为version
    设置baseurl以在(获取文档1的URL)中使用带有regexp和字符串结果的“\\1”来查找文本“^(http)?://.*?/”,例如获取基(根)URL。http://www.example.com
    设置relativebaseurl以使用regexp和字符串结果在(获取文档1的URL)中查找文本“^(http)?://.*)/”--获取相对URL的基础(当前目录),例如。http://www.example.com/exa/mple/
    设置cursource以获取文档1的源--获取当前页面的源
    将链接列表设置为“”
    重复i从1到扩展项的数量——检查每个扩展的链接(href)。
    
    将searchresult设置为查找文本(“”)将其分配给Safari中的热键以便于批量下载。

    链接脚本使用Python下载文件。但是,由于cookie问题,David要求通过Safari下载文件。下载
    Safari PDF Grabber
    脚本并检查其内容。
    download.py
    脚本负责下载MacScripter的补充语录:“PDF链接然后由位于AppleScript捆绑包内的Python脚本处理,该脚本使用urllib将PDF文档下载到下载文件夹。”@Anne Whoops!我没有看到。对不起!:(嗯),我喜欢选项2,但是现在我遇到了一个问题,显然狮子在Safari中摆脱了下载窗口。所以第一次下载的自动化证明是有挑战性的。Safari下载管理器确实在狮子里面丢失了。我目前还不知道该如何解决这个问题。你可以考虑使用另一个仍然使用的浏览器。经典的下载管理器。我可能不得不这样做。不过,你的JavaScript想法可能对我仍然有效。关于将Safari设置为不以本机方式打开某些文件类型,而是始终下载它们,我有一个公开的问题。感谢你的帮助!Safari 13似乎已经打破了默认设置写入com.apple.Safari DownloadsPath
    .Know新的偏好可能是什么?
    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
    
    do shell script "defaults write com.apple.safari DownloadsPath -string \"/Users/Anne/Desktop\""
    
    do shell script "defaults write com.apple.Safari AutoOpenSafeDownloads -boolean NO"
    
    ## 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