Applescript从Http服务器下载文件零星的结果

时间:2011-09-16 14:57:08

标签: applescript

我编写了一个从Http服务器下载文件的脚本,但结果非常零散。如果我连续运行三次,它可能会工作两次并且错误一次或根本不工作并返回不同的错误。

我得到的一些错误是:

下载目标网址时出错(-609) Url Access Scripting出错:连接无效。

下载目标网址(-31040)时出错 URL访问脚本出错:发生了类型-31040的错误。

try
    set theFileURL to "http://ftp2.nflfilmstv.com/filmsint/ftp-inet/Team/110915_game_preview_phi_atl_3200k.mp4" as text

    set TID to AppleScript's text item delimiters
    set AppleScript's text item delimiters to "/"
    set theFile to text item -1 of theFileURL
    set AppleScript's text item delimiters to TID

    set theFilePath to "Macintosh HD:Users:rgilkes:Desktop:" & theFile as text

    tell application "URL Access Scripting" to download theFileURL to file theFilePath with progress
    on error ErrorMessage number ErrorNumber
        display alert "Error with downloading target URL (" & ErrorNumber & ")" message ErrorMessage
end try

有没有更好的方法通过AppleScript下载文件或我的编码不好?

1 个答案:

答案 0 :(得分:1)

啊,谢谢预览!我原来是费城人,对费城体育仍充满热情。我希望我不会帮助猎鹰队的球迷。至少不是本周! ;)

无论如何,尽管URLAccessScripting不是最可靠的下载方式,但您的代码看起来还不错。实际上从10.7开始,它甚至不再包含在操作系统中。卷曲是另一种选择,通常是稳定的。但是你不会得到一个进度窗口。试试这个。看看它是否更稳定。它会在完成时告诉你。

set theFileURL to "http://ftp2.nflfilmstv.com/filmsint/ftp-inet/Team/110915_game_preview_phi_atl_3200k.mp4"

set {TID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "/"}
set theFile to text item -1 of theFileURL
set AppleScript's text item delimiters to TID

set theFilePath to (path to desktop as text) & theFile

try
    do shell script "curl " & quoted form of theFileURL & " -o " & quoted form of POSIX path of theFilePath
    display dialog "The download is finished!" buttons {"OK"} default button 1 with icon note giving up after 5
on error theError
    display dialog "Error downloading the file:" & return & theFile & return & return & theError buttons {"OK"} default button 1 with icon 0 giving up after 5
end try