It's generally a good idea to use an user-agent string if you're accessing servers you don't know; some of them don't especially like curl.
The Opera string is one I've used for years, but you can use any one you want.
This handler strives to make all of curl's used options easy to read and understand. (Not all options in the comment listing are used.)
If you get to the point where you want to download multiple items then you'll want to learn how to use a config file.
See -K, --config in the man page. The documentation for this isn't just wonderful, so holler at me if you want some help.
-------------------------------------------------------------------------------------------
set destinationDirectory to "~/Downloads"
# Shell command for handler to extract the downloaded file name from verbose curl output.
set cmdStr to "| awk 'BEGIN { FS = \"/\" }; /Content-Location/ { print $NF }'"
# Download the file
set downloadedFileName to downloadFileToDirectory(destinationDirectory, remoteURL, cmdStr)
set downloadedFilePath to expandTildeInPath(destinationDirectory) & "/" & downloadedFileName
set downloadedFileAlias to alias POSIX file downloadedFilePath
-------------------------------------------------------------------------------------------
--» HANDLERS
-------------------------------------------------------------------------------------------
# --location == -L == Follow redirects
# --remote-name-all == No Alternate == Use the remote names of the downloaded files.
# --silent == -s == No progress bar
# --user-agent == -A == Employ a user-agent-string
# --verbose == -v == Verbose output
-------------------------------------------------------------------------------------------
on downloadFileToDirectory(destinationDirectory, remoteURL, cmdStr)
if destinationDirectory starts with "~/" then
set destinationDirectory to expandTildeInPath(destinationDirectory) of me
end if
set curlCMD to items 1 thru -2 of {¬
"curl", ¬
"-v", ¬
"-A 'Opera/9.70 (Linux ppc64 ; U; en) Presto/2.2.1'", ¬
"--remote-name-all", ¬
remoteURL, ¬
"2>&1", ¬
""}
# “2>&1” --> allows curl's verbose output to be seen by AppleScript.
set {oldTIDS, AppleScript's text item delimiters} to {AppleScript's text item delimiters, " "}
set curlCMD to curlCMD as text
set AppleScript's text item delimiters to oldTIDS
set shCMD to "cd " & destinationDirectory & ";" & linefeed & curlCMD
if cmdStr ≠ false and cmdStr ≠ "" then
set shCMD to shCMD & space & cmdStr
end if
do shell script shCMD
end downloadFileToDirectory
-------------------------------------------------------------------------------------------
on expandTildeInPath(thePath)
tell application "System Events" to return POSIX path of disk item thePath
end expandTildeInPath
-------------------------------------------------------------------------------------------