ftp and connecting to server

I need help writing a script to do the following:

  1. connect to an ftp site ftp.whatever.com using rbrowser lite or cyberduck
  2. promopt the user to enter the file name or a list of file name they want to download from the ftp site.
  3. prompt the user where he wants files to download to.
  4. upload to server afp://someserver.com
  5. let the user know when done.

any suggestions will help. Thanks.

Hi,

quick and dirty without any FTP-client,
assuming the server volume has been mounted already

property ftpserver : "[url=ftp://ftp.myServer.com]ftp.myServer.com[/url]" --ftp server 
-- property ftppath : "/path/to/" --the path to the file on the server
property server_username : "¢¢¢¢¢" --server_username
property server_password : "¢¢¢¢¢" --server_password

set ftppath to text returned of (display dialog "Enter path of FTP-Server (slash separated):" default answer "/")
if ftppath does not end with "/" then set ftppath to ftppath & "/"
set servername to ftpserver & ftppath
set theFiles to paragraphs of (do shell script "curl -l " & servername & " -u " & server_username & ":" & server_password)
set DLfiles to choose from list theFiles with prompt "choose one or multiple files to download:" with multiple selections allowed
set destfolder to (choose folder with prompt "choose destination folder")
repeat with i in DLfiles
	set serverURL to quoted form of (ftpserver & ftppath & i)
	set destPath to quoted form of ((POSIX path of destfolder) & i)
	do shell script "curl -o " & destPath & " -u " & server_username & ":" & server_password & " " & serverURL
end repeat
display dialog ((count DLfiles) as string) & " file(s) downloaded" buttons {"Done"} default button 1

This is really ausome, without FTP clients! wow!
connects fine, downloads files without a problem. and correct if I am wrong, I noticed files download a lot faster!

not to make things harder, but noticed files downloaded are zipped files. those files need to unzipped before uploading to the mounted server.
Also, I am dealing with multiple ftp sites each with a different user and password, any suggestions how to handle that.

thanks.
bill

Hi Bill,

unstuffing the files is possible, but I couldn’t find the UNIX way to do it.
The unzip command does unstuff files, but just files not folders or packages,
i.e. the folders have been unstuffed but I couldnt tell unzip to put the expanded files into the right folders,
or I’m too stupid to find it after studying the man page.

Maybe one of the UNIX specialists can tell us how to do it :wink:

Anyway so I used the free StuffIt Expander to do the job,
which is even scriptable. All files and zip archives will be downloaded
to a temporary folder, which will be deleted at the end of the script.

The property lines at the beginning are lists so you can add futher servers.
The path(s) on the server(s) won’t be asked for anymore, there is also a property

property ServerList : {"ftp.myServer1.com", "[url=ftp://ftp.myServer2.com]ftp.myServer2.com[/url]", "[url=ftp://ftp.myServer3.com]ftp.myServer3.com[/url]"} --ftp server 
property pathList : {"/pub/jpg/", "/pub/gif/", "/pub/png/"}
property usernameList : {"¢¢¢¢¢", "¢¢¢¢¢", "¢¢¢¢¢"} --server_username
property passwordList : {"¢¢¢¢¢", "¢¢¢¢¢", "¢¢¢¢¢"} --server_password

set tempFolder to POSIX path of ((path to temporary items as Unicode text) & "curl_temp_folder:")
try
	do shell script "rm -rf " & quoted form of tempFolder
end try
do shell script "mkdir " & quoted form of tempFolder

repeat with i from 1 to count ServerList
	set ftpserver to item i of ServerList
	set ftppath to item i of pathList
	set server_username to item i of usernameList
	set server_password to item i of passwordList
	
	if ftppath does not end with "/" then set ftppath to ftppath & "/"
	set servername to ftpserver & ftppath
	set theFiles to paragraphs of (do shell script "curl -l " & servername & " -u " & server_username & ":" & server_password)
	set DLfiles to choose from list theFiles with prompt "choose one or multiple files to download:" with multiple selections allowed
	set destfolder to (choose folder with prompt "choose destination folder")
	repeat with i in DLfiles
		set serverURL to quoted form of (servername & i)
		set destPath to quoted form of (tempFolder & i)
		do shell script "curl -o " & destPath & " -u " & server_username & ":" & server_password & " " & serverURL
	end repeat
end repeat
tell application "Finder" to set fileList to items of (POSIX file tempFolder as alias)
set c to count fileList
repeat with i in fileList
	if (name extension of (info for (i as alias)) is "zip") then
		tell application "StuffIt Expander" to expand (i as alias) to destfolder
	else
		tell application "Finder" to move (i as alias) to destfolder
	end if
end repeat
quit application "StuffIt Expander"
display dialog (c as string) & " file(s) downloaded" buttons {"Done"} default button 1
try
	do shell script "rm -rf " & quoted form of tempFolder
end try

Thanks again Stefan for the suggested solution. I have tried to compile your code but running into some syntax errors. can you please advice.

The compiler complains about this line:

tell application “StuffIt Expander” to expand (i as alias) to destfolder

I get the message Expect end of line but found “to.”

can you please advice.

Hi Bill,

is StuffIt Expander installed on your machine? It’s not a part of Tiger.
But if you don’t get a dialog to locate the application, it would be.
I use the version of StuffIt Deluxe, which might be different to the free version.

I’ve tested the script before having posted it, and it works flawless on my machine

is StuffIt Expander installed on your machine? It’s not a part of Tiger.
yes. It’s under utilities in the application folder. so you think the deluxe version might be different than this as far as being scribtable!

I don’t know, it’s only a guess.
Can you read the directory of StuffIt Expander, what’s written about the expand command

Can you read the directory of StuffIt Expander, what’s written about the expand command
there was nothing much in my folder the command expand. I have done some online search and came up with the some ideas, but would need help putting them together.

tell application “Finder”
open the selection using application file “StuffIt Expander.app” of folder “Utilities” of folder “Applications” of startup disk
end tell

other script that might help

tell application “StuffIt Expander” to expand {alias tempFile} into alias theAttachmentPath with delete originals

thanks.

I’m sorry, I meant dictionary not directory. You can open the dictionary of StuffIt in Script Editor with shift-cmd-O

Your sample scripts do the same as my line above (except the delete originals parameter, which we don’t need)

ok. here’s what the dictionary for stuffit expander in script editor reads:

Expand‚v : Expand the specified list of documents/folders/disks
Expand list of alias : List of documents/folders/disks to Expand
[Delete Original boolean] : Should the files be deleted after they are expanded?
[Destination alias] : Alternate destination for the files
→ integer : Returns the number of files that were successfully expanded.

I made some changes in the code, but haven’t fixed the problem!

Thanks Bill,

Your directory is indeed a little different,
expand expects a list of alias files
and the keyword destination instead of to

tell application "StuffIt Expander" to expand {i as alias} destination destfolder

consider the curly brackets, which coerce the alias to a list

i am sure you are tired of me now!

tell application “StuffIt Expander” to expand {i as alias} destination destfolder

when compiled, I get a syntax error “expected end of line, but found identifier destination”

I run the script with this line
tell application “StuffIt Expander” to expand {i as alias}

but downloaded files are not in the destination I want them to be!

No, don’t worry :slight_smile:

Maybe Destination is case sensitive, so try it with a capital D

Maybe Destination is case sensitive, so try it with a capital D

nop. that didn’t work!

This is odd.
I’m sorry not to be able to test it on my machine,
because I don’t want to install a different version.
BTW: Have you installed the current version of StuffIt?
Maybe a newer one works better.

I have download the latest version of Stuffit 11.0.2, and guess what, with some modification to the identifier “into” instead of “to”. It works.
one thing that still bothers me about the script is I want to have a list of ftp server user names and pasword, and the promopt the user asking him which server he wants to connect to, or a list of servers to connect to.

Here is the final script:
property ServerList : {“ftp.myServer1.com”, “ftp.myServer2.com”, “ftp.myServer3.com”} --ftp server
property pathList : {“/pub/jpg/”, “/pub/gif/”, “/pub/png/”}
property usernameList : {“¢¢¢¢¢”, “¢¢¢¢¢”, “¢¢¢¢¢”} --server_username
property passwordList : {“¢¢¢¢¢”, “¢¢¢¢¢”, “¢¢¢¢¢”} --server_password
set tempFolder to POSIX path of ((path to temporary items as Unicode text) & “curl_temp_folder:”)
try
do shell script "rm -rf " & quoted form of tempFolder
end try
do shell script "mkdir " & quoted form of tempFolder

repeat with i from 1 to count ServerList
set ftpserver to item i of ServerList
set ftppath to item i of pathList
set server_username to item i of usernameList
set server_password to item i of passwordList

if ftppath does not end with "/" then set ftppath to ftppath & "/"
set servername to ftpserver & ftppath
set theFiles to paragraphs of (do shell script "curl -l " & servername & " -u " & server_username & ":" & server_password)
set DLfiles to choose from list theFiles with prompt "choose one or multiple files to download:" with multiple selections allowed
set destfolder to (choose folder with prompt "choose destination folder")
repeat with i in DLfiles
	set serverURL to quoted form of (servername & i)
	set destPath to quoted form of (tempFolder & i)
	do shell script "curl -o " & destPath & " -u " & server_username & ":" & server_password & " " & serverURL
end repeat

end repeat
tell application “Finder” to set fileList to items of (POSIX file tempFolder as alias)
set c to count fileList
repeat with i in fileList
if (name extension of (info for (i as alias)) is “zip”) then

	tell application "StuffIt Expander" to expand {i as alias} into destfolder
	
else
	tell application "Finder" to move (i as alias) to destfolder
end if

end repeat
quit application “StuffIt Expander”
display dialog (c as string) & " file(s) downloaded" buttons {“Done”} default button 1
try
do shell script "rm -rf " & quoted form of tempFolder
end try

This confusion of StuffIt is weird :slight_smile:

Here is a version of the script, which asks for the server(s)

property NameList : {"Server1", "Server2", "Server3"}
property ServerList : {"ftp.myServer1.com", "[url=ftp://ftp.myServer2.com]ftp.myServer2.com[/url]", "[url=ftp://ftp.myServer3.com]ftp.myServer3.com[/url]"} --ftp server
property pathList : {"/pub/jpg/", "/pub/gif/", "/pub/png/"}
property usernameList : {"¢¢¢¢¢", "¢¢¢¢¢", "¢¢¢¢¢"} --server_username
property passwordList : {"¢¢¢¢¢", "¢¢¢¢¢", "¢¢¢¢¢"} --server_password

set tempFolder to POSIX path of ((path to temporary items as Unicode text) & "curl_temp_folder:")
try
	do shell script "rm -rf " & quoted form of tempFolder
end try
do shell script "mkdir " & quoted form of tempFolder

set chosenServer to (choose from list NameList with prompt "choose one or multiple servers" with multiple selections allowed)
if chosenServer is false then return

repeat with i from 1 to count chosenServer
	repeat with j from 1 to count NameList
		if item i of chosenServer is item j of NameList then exit repeat
	end repeat
	set ftpserver to item j of ServerList
	set ftppath to item j of pathList
	set server_username to item j of usernameList
	set server_password to item j of passwordList
	
	if ftppath does not end with "/" then set ftppath to ftppath & "/"
	set servername to ftpserver & ftppath
	set theFiles to paragraphs of (do shell script "curl -l " & servername & " -u " & server_username & ":" & server_password)
	set DLfiles to choose from list theFiles with prompt "choose one or multiple files to download:" with multiple selections allowed
	set destfolder to (choose folder with prompt "choose destination folder")
	repeat with i in DLfiles
		set serverURL to quoted form of (servername & i)
		set destPath to quoted form of (tempFolder & i)
		do shell script "curl -o " & destPath & " -u " & server_username & ":" & server_password & " " & serverURL
	end repeat
end repeat
tell application "Finder" to set fileList to items of (POSIX file tempFolder as alias)
set c to count fileList
repeat with i in fileList
	if (name extension of (info for (i as alias)) is "zip") then
		
		tell application "StuffIt Expander" to expand {i as alias} into destfolder
		
	else
		tell application "Finder" to move (i as alias) to destfolder
	end if
end repeat
quit application "StuffIt Expander"
display dialog (c as string) & " file(s) downloaded" buttons {"Done"} default button 1
try
	do shell script "rm -rf " & quoted form of tempFolder
end try

the script works a lot better now. thanks.
I have ran the script several times, I noticed sometimes I have to run the script twice to make sure it downloads the files.
Also, I have noticed since the server where I am copying the files to, deletes the downloaded files after they are uploaded.
so, I hope this is a small and final modification to this script, never thought this script will take that long:
can we have a prompt that will ask the user after the files are downloaded is he/she wishes to copy the files from folder1 to a server or another folder(folder2), and after they are done copying, if they want to delete the files in folder1.

I have really learned a lot working on this script.

this is quite easy but only if we can copy all files from folder 1 to folder 2,
because sometimes StuffIt renames the extracted files a bit, so the file list (variable fileList) is probably wrong.

How do you specify the second folder?