The script below was successfully tested on Mac OS X 10.5. It’s far from being perfect as far as the code is concerned, but this is due to the fact that all the while I am watching «Smilla’s Sense of Snow» on TV
on run
set server to "ftp://ftp.apple.com"
set dir to "/developer/Java/"
-- downloading the directory listing/ file names with the -l option of curl
set command to "curl -l " & quoted form of (server & dir)
set filenames to paragraphs of (do shell script command)
set fileinfos to {}
set datestamps to {}
-- looping over the file names to download file info only with the -I option of curl
repeat with filename in filenames
set command to "curl -I " & quoted form of (server & dir & filename)
try
set fileinfo to paragraphs of (do shell script command)
-- converting the date string from curl into a comparable format
set moddate to (characters 16 through -1 of (item 1 of fileinfo)) as Unicode text
set compdate to my convdatestr(moddate)
set fileinfos to fileinfos & {{filename as Unicode text, compdate}}
set datestamps to datestamps & compdate
end try
end repeat
-- sorting the dates
set sortvalues to my bubblesort(datestamps)
set latestdate to last item of sortvalues
-- downloading the latest file
repeat with fileinfo in fileinfos
if item 2 of fileinfo is equal to latestdate then
set filename to item 1 of fileinfo
try
set command to "curl -O " & quoted form of (server & dir & filename)
log command
do shell script command
end try
end if
end repeat
end run
on convdatestr(datestr)
set olddelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to {space}
set txtitems to text items of datestr
set AppleScript's text item delimiters to olddelims
set {strday, strmonth, stryear} to {item 2, item 3, item 4} of txtitems
set strmonth to ((offset of strmonth in "JanFebMarAprMayJunJulAugSepOctNovDec") div 3) + 1
if strmonth < 10 then set strmonth to "0" & strmonth
set {strhour, strmin, strsec} to {characters 1 through 2, characters 4 through 5, characters 7 through 8} of (item 5 of txtitems)
set newdate to (stryear & strmonth & strday & (strhour as Unicode text) & (strmin as Unicode text) & (strsec as Unicode text)) as number
end convdatestr
on bubblesort(theList)
-- defining an internal script makes for faster run times!
script bs
property alist : theList
end script
set theCount to length of bs's alist
if theCount < 2 then return bs's alist
repeat with indexA from theCount to 1 by -1
repeat with indexB from 1 to indexA - 1
if item indexB of bs's alist > item (indexB + 1) of bs's alist then
set temp to item indexB of bs's alist
set item indexB of bs's alist to item (indexB + 1) of bs's alist
set item (indexB + 1) of bs's alist to temp
end if
end repeat
end repeat
return bs's alist
end bubblesort