Download a file from a URL

Howdy all,

I’m back to work again :stuck_out_tongue: This time, its to download a file from a URL.

Here’s what I have so far…


tell application "Finder"
set mypath to (path to me)
	set myfold to ((mypath as string) & "Contents:Resources:Downloads")
set filelist to {"item 1", "Item 2"}
display dialog "This will download the appropriate files"
set choosefile to (choose from list filelist as list with prompt "What files do you want to find?") as Unicode text
	if choosefile is false then return
	if choosefile is "item 1" then
		tell application "URL Access Scripting"
			download "http://www.mysite.com/files/file1.zip" to myfold with unpacking and progress
		end tell
else
--ect ect
end if
end tell

Obviously, I’ve changed names a bit, and cut out other sections.
The thing is, when I run this, I get an “An error of type -1 has occured” error. The problem is, I don’t know what that is. I thought it ment that the file couldn’t be found, but I’ve double checked the file path, and its correct. >.<

Am I perhaps not doing the correct command?

Thanks!
-Parrot

You’re providing URL Access Scripting with a folder when it expects a file (for the to parameter).

This works fine:

display dialog "This will download the appropriate files"

set downloadFolder to (path to desktop as Unicode text)

choose from list {"item 1", "Item 2"} as list with prompt "What files do you want to find?"
set chooseFile to result as Unicode text

if chooseFile is "false" then
	error number -128 -- User canceled
else if chooseFile is "item 1" then
	tell application "URL Access Scripting"
		download "http://files.macscripter.net/bbs/images/SE_bundle_resources-17424.png" to (downloadFolder & "test.png") -- with unpacking
	end tell
else
	--ect ect
end if

Also, progress doesn’t work in OS X.

So I cannot have it download a folder? Or is there an alternate command that I need to use that I’m not picking out of the script you provied?

In your first example you were downloading a file, but you didn’t provide a name for the downloaded file.

I don’t believe there is a way to download a directory with that URL osax. (You can get a listing using with download directory though.) You might look into getting and using the UNIX tool “wget” on OS X.

Thanks, providing a name did it :slight_smile:

Though, for whatever reason, it doesn’t unpack the resulting file…

That’s probably limited to certin compression/packing schemes.