download via ftp or curl, prevent file overwrite, add file comments

This is a working script in this order
:: Upon email arrival, (there’s a filter/rule that calls this script)
:: Parse email and get file name information
:: Check to see if file already exists in local folder and prevent overwrite by changing the name of incoming file
:: Download file via ftp (or curl)
:: Add comments to downloaded file

Thanks to everyone that helped in the various stages of this script.


global the_newimage

using terms from application "Mail"
	on perform mail action with messages theMessages
		repeat with thisMail in theMessages
			tell thisMail
				set the_subject to get subject
				set the_sender to get sender
				set the_body to (source of thisMail as text)
			end tell
		end repeat
		--the first line of the email body looks like this
		--**Hairy|hairys@email.com|test.gif**
		set the_info to the_body
		set AppleScript's text item delimiters to {"**"}
		set the_info to second text item of (the_info as string)
		set AppleScript's text item delimiters to {"|"}
		set the_name to first text item of (the_info as string)
		set the_email to second text item of (the_info as string)
		set the_image to third text item of (the_info as string)
		set the_newimage to third text item of (the_info as string)
		check_filename(the_image)
		comments(the_name, the_email)
	end perform mail action with messages
end using terms from

property the_path : "Alaska:Users:sl:Desktop:EH:" --replace with your local path

on check_filename(the_image)
	set the_imagepath to the_path & the_image
	tell application "Finder"
		repeat
			if not (file the_imagepath exists) then
				my download(the_image)
				exit repeat
			else
				--adopted from sitcom
				--http://bbs.applescript.net/viewtopic.php?id=12878
				set AppleScript's text item delimiters to "."
				set BaseFilename to text item 1 of the_newimage
				set FileExt to text item 2 of the_newimage
				set DateStamp to do shell script "date +%m%d%y" --You may want to remove '_%y' 
				set the_newimage to BaseFilename & "_" & DateStamp & "." & FileExt
				set the_imagepath to the_path & the_newimage
			end if
		end repeat
	end tell
end check_filename

property ftp_server : "[url=http://www.yoururl.com]www.yoururl.com[/url]" -- replace with your url
property ftp_path : "/yoururl.com/upload_data/" -- replace with path to ftp file directory where files lie
property ftp_username : "username" -- your username, for ftp or curl access
property ftp_password : "passwd" -- your username, for ftp or curl access
property ftp_file : "" -- leave blank
property ftp_local : "" -- leave blank

--replace paths (ie ~/Desktop/EH/) with your local path
on download(the_image)
	set ftp_file to the_image
	set ftp_local to the_newimage
	do shell script "ftp -o ~/Desktop/EH/" & ftp_local & " ftp://" & ftp_username & ":" & ftp_password & "@" & ftp_server & ftp_path & ftp_file
	--if you have a folder with a space in its name, use single quotes /Desktop/'hairy smith'/
	--curl sample below
	--do shell script "curl -o ~/Desktop/'hairy smith'/" & ftp_local & " http://" & ftp_server & ftp_path & ftp_file
end download

on comments(the_name, the_email)
	set the_filename to (the_path & the_newimage) as alias
	tell application "Finder" to set comment of the_filename to the_name & return & the_email & return & (current date) as string
end comments

Regarding AS’s text item delimiters, I recommend this:

set originalDelim to AppleScript's text item delimiters
set AppleScript's text item delimiters to "*"
-- insert code here
set AppleScript's text item delimiters to originalDelim