• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Downloading a file from the internet with a URL
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Downloading a file from the internet with a URL


  • Subject: Re: Downloading a file from the internet with a URL
  • From: Shane Stanley <email@hidden>
  • Date: Fri, 13 May 2016 11:24:47 +1000

On 13 May 2016, at 1:55 AM, Stockly, Ed <email@hidden> wrote:

Any suggestions?

Here's an AppleScriptObjC approach. The main advantage is that it uses AppleScript's progress properties to show the progress of the download:

use framework "Foundation"
use scripting additions
property fileDest : ""

set fileDest to "/Users/Shane/Desktop/Testdownload.zip" -- destination path
set theURL to current application's class "NSURL"'s URLWithString:"http://www.macosxautomation.com/applescript/apps/ASObjC_Explorer_4_Stuff.zip" -- file to download
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:

-- 
Shane Stanley <email@hidden>
<www.macosxautomation.com/applescript/apps/>

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Follow-Ups:
    • Re: Downloading a file from the internet with a URL
      • From: Shane Stanley <email@hidden>
References: 
 >Downloading a file from the internet with a URL (From: "Stockly, Ed" <email@hidden>)

  • Prev by Date: Re: Downloading a file from the internet with a URL
  • Next by Date: Re: Downloading a file from the internet with a URL
  • Previous by thread: Downloading a file from the internet with a URL
  • Next by thread: Re: Downloading a file from the internet with a URL
  • Index(es):
    • Date
    • Thread