Applescript - 下载脚本
Applescript - Download Script
几天前我做了这个下载脚本:
do shell script "curl -L -o ~/Desktop/file.dmg 'https://download.mozilla.org/?product=firefox-27.0.1-SSL&os=osx&lang=en-GB' > ~/Desktop/status 2>&1 &"
set fileSize to 0
set curTransferred to 0
set curProgress to 0
repeat until curProgress = "100"
try
set lastLine to paragraph -1 of (do shell script "cat ~/Desktop/status")
set curProgress to word 1 of lastLine
set fileSize to word 2 of lastLine
set curTransferred to word 4 of lastLine
tell me
display dialog "Downloading. Please wait...
Status: " & curTransferred & " of " & fileSize & " (" & curProgress & "%)" buttons {"Refresh", "cancel"} giving up after 4
if the button returned of the result is "cancel" then return
end tell
on error
display dialog "Something went wrong when downloading the file. If you would like to restart the download then press the button 'Retry'" buttons {"Quit", "Retry"} with icon 0
end try
end repeat
它有效,但我仍然遇到一些问题(确切地说是 3 个)
第一个问题:
有没有办法只显示 CurTransferred、FileSize 和 Curprogress 而无需每 4 秒为 display/refresh(新)信息创建一个新的 window?
第二个问题:
applescript 中有没有办法制作一种 'Pause' 按钮(在此脚本中)?因此,当单击暂停时,下载将停止,并会打开一个带有 'Continue' 或 'Start' 按钮的新对话框?
问题三:
有没有办法将 ~/Desktop/file.dmg
更改为这样的代码:
set downloadlocation to (choose folder)
然后是类似的东西:脚本中的 path to (downloadlocation)
而不是 ~/Desktop/file.dmg
如果您知道其中一个(或所有)问题的答案,请毫不犹豫地回答这个问题!
感谢阅读,
乔特
PS:也许检查 Internet 连接会很酷,但不是必需的。
第一个问题:
没有
第二个问题:
没有
问题三
set downloadLocation to choose folder with prompt "choose download location"
do shell script "curl -L -o " & quoted form of (POSIX path of downloadLocation & "file.dmg") & " 'https://download.mozilla.org/?product=firefox-27.0.1-SSL&os=osx&lang=en-GB' > ~/Desktop/status 2>&1 &"
问题一和问题二无法使用纯 AppleScript 解决,因为 AS 在单个线程中同步工作。由于 shell 脚本设置为不等待完成,因此除了从状态文件中读取输出外,无法控制进度。
几天前我做了这个下载脚本:
do shell script "curl -L -o ~/Desktop/file.dmg 'https://download.mozilla.org/?product=firefox-27.0.1-SSL&os=osx&lang=en-GB' > ~/Desktop/status 2>&1 &"
set fileSize to 0
set curTransferred to 0
set curProgress to 0
repeat until curProgress = "100"
try
set lastLine to paragraph -1 of (do shell script "cat ~/Desktop/status")
set curProgress to word 1 of lastLine
set fileSize to word 2 of lastLine
set curTransferred to word 4 of lastLine
tell me
display dialog "Downloading. Please wait...
Status: " & curTransferred & " of " & fileSize & " (" & curProgress & "%)" buttons {"Refresh", "cancel"} giving up after 4
if the button returned of the result is "cancel" then return
end tell
on error
display dialog "Something went wrong when downloading the file. If you would like to restart the download then press the button 'Retry'" buttons {"Quit", "Retry"} with icon 0
end try
end repeat
它有效,但我仍然遇到一些问题(确切地说是 3 个) 第一个问题:
有没有办法只显示 CurTransferred、FileSize 和 Curprogress 而无需每 4 秒为 display/refresh(新)信息创建一个新的 window?
第二个问题:
applescript 中有没有办法制作一种 'Pause' 按钮(在此脚本中)?因此,当单击暂停时,下载将停止,并会打开一个带有 'Continue' 或 'Start' 按钮的新对话框?
问题三:
有没有办法将 ~/Desktop/file.dmg
更改为这样的代码:
set downloadlocation to (choose folder)
然后是类似的东西:脚本中的 path to (downloadlocation)
而不是 ~/Desktop/file.dmg
如果您知道其中一个(或所有)问题的答案,请毫不犹豫地回答这个问题!
感谢阅读,
乔特
PS:也许检查 Internet 连接会很酷,但不是必需的。
第一个问题:
没有
第二个问题:
没有
问题三
set downloadLocation to choose folder with prompt "choose download location"
do shell script "curl -L -o " & quoted form of (POSIX path of downloadLocation & "file.dmg") & " 'https://download.mozilla.org/?product=firefox-27.0.1-SSL&os=osx&lang=en-GB' > ~/Desktop/status 2>&1 &"
问题一和问题二无法使用纯 AppleScript 解决,因为 AS 在单个线程中同步工作。由于 shell 脚本设置为不等待完成,因此除了从状态文件中读取输出外,无法控制进度。