use framework "Foundation"
use scripting additions
property fileDest : ""
set fileDest to "/Users/Shane/Desktop/Testdownload.zip" -- destination path
set theSession to current application's NSURLSession's sessionWithConfiguration:(missing value)
theSession's setDelegate:me -- so script gets told when it's done
set theTask to theSession's downloadTaskWithURL:theURL
theTask's resume() -- start the task
repeat
if theTask's state() = 3 then exit repeat -- 3 means complete
set AppleScript's progress total steps to theTask's countOfBytesExpectedToReceive()
set AppleScript's progress completed steps to theTask's countOfBytesReceived()
delay 1
end repeat
if theTask's |error|() is not missing value then error (theError's localizedString() as text)
set theResponse to theTask's response()
set theCode to theResponse's statusCode()
if theCode = 200 then
display dialog "File successfully downloaded"
else
display dialog "Result: " & (theCode as text) & ", " & (current application's NSHTTPURLResponse's localizedStringForStatusCode:theCode) as text
end if
on URLSession:theSession downloadTask:theTask didFinishDownloadingToURL:anNSURL
-- move temp file into place
current application's NSFileManager's defaultManager()'s moveItemAtPath:(anNSURL's |path|()) toPath:fileDest |error|:(missing value)
end URLSession:downloadTask:didFinishDownloadingToURL: