AppleScript to Upload Files to a FTP Server

Hello,

This is my first contact with AppleScript and my first post here.

I need an Applescript that upload (FTP server) each file I copy or edit in a folder.
I’ve found a AppleScript that upload into a FTP server all new and modified files of a folder.

My problem is that only run manually. I’ve attached it to a Folder Action but the Applescript doesn’t run.

The code is:

(*
upload
Uploads the given file or folder to the given remote ftp folder using "curl".
If you need user/password to login to such server, append it to the URL. Eg:
ftp://user:password@ftp.server.com/dir/

Parameters: filePath: file path, alias, posix path (a file or folder)
remoteDir: ftp directory (eg: "ftp://ftp.server.com/foo/" or "ftp://user:password@ftp.server.com/dir/")

Example: upload(alias "path:to:folder:/", "ftp://user:password@domain.com/public_html/temp/")
*)

upload("path:to:folder:", "ftp://user:password@domain.com/public_html/temp/")

to upload(filePath, remoteDir)
	global baseLocalFolder, baseRemoteFolder, ftpHome, ftpDir
	script nesteed
		to guessNewDir(f) -- "/path/to/footest" --> /footest
			set prevTids to AppleScript's text item delimiters
			set AppleScript's text item delimiters to POSIX path of parent's baseLocalFolder
			set f to item -1 of f's text items
			set AppleScript's text item delimiters to prevTids
			return f
		end guessNewDir
		to breakURL(d) --> "ftp://user:pass@ftp.server.com/html/docs/" --> {"ftp://user:pass@ftp.server.com", "/html/docs"}
			set prevTids to AppleScript's text item delimiters
			set AppleScript's text item delimiters to "/"
			set ftpHome to "" & items 1 thru 3 of d's text items
			try
				set ftpDir to "/" & items 4 thru -1 of d's text items
			on error
				set ftpDir to "/"
			end try
			set AppleScript's text item delimiters to prevTids
			return {ftpHome, ftpDir}
		end breakURL
		
		to processUnknownItem(f, remoteDir)
			set f to f as text
			if f ends with ":" then
				processFolder(f, remoteDir)
			else
				do shell script "curl -T " & quoted form of POSIX path of f & space & quoted form of remoteDir
			end if
		end processUnknownItem
		to processFolder(f, remoteDir)
			set newDir to guessNewDir(POSIX path of f) --> "/footest"
			try --> avoid existing dirs
				if newDir is not "" then do shell script "curl -Q " & quoted form of ("MKD " & parent's ftpDir & newDir) & space & parent's ftpHome
			end try
			set itemList to list folder alias f without invisibles
			repeat with i in itemList
				processUnknownItem(alias (f & i), parent's ftpHome & parent's ftpDir & newDir)
			end repeat
		end processFolder
	end script
	set wasError to false
	try
		set filePath to filePath as Unicode text
		if filePath does not contain ":" then set filePath to POSIX file filePath as Unicode text
		if remoteDir does not end with "/" then set remoteDir to remoteDir & "/"
		if filePath ends with ":" then --> mirror dir
			-- MAKE DIRECTORY "TEST" IN EXISTING "/HTML/DOCS"
			-- curl -Q "MKD /html/docs/test" ftp://user:pass@ftp.server.com
			
			set baseLocalFolder to filePath
			set baseRemoteFolder to remoteDir
			set {ftpHome, ftpDir} to breakURL(remoteDir) of nesteed --> {"ftp://user:pass@ftp.server.com", "/html/docs"}
			
			processFolder(filePath, remoteDir) of nesteed
		else
			do shell script "curl -T " & quoted form of POSIX path of filePath & space & quoted form of remoteDir
		end if
	on error msg number n
		set wasError to true
	end try
	set baseLocalFolder to missing value
	set baseRemoteFolder to missing value
	set ftpHome to missing value
	set ftpDir to missing value
	if wasError then error msg number n
	return
end upload

property |version| : 1.0
property author : "Pescados Software @ PescadosWeb.com"
property |date| : date "sábado, 17 de julio de 2004, 22:20:27"
property license : "freeware, open-source"

If I replace the first line:

upload("path:to:folder:", "ftp://user:password@domain.com/public_html/temp/")

by:

on adding folder items to this_folder after receiving added_items
	upload("path:to:folder:", "ftp://user:password@domain.com/public_html/temp/")
end adding folder items to

The Applescript runs automatically only when I add a new file but not when I edit one of them.

Can someone help me? :slight_smile:

Rename file after editing, or drag it out of HOT folder and drag back

What should “path:to:folder:” be?

If it’s in a hot folder shouldn’t this get set automatically?

If the items of the new folder are to be uploaded no?

Here, the OP somewhere found a ready-made upload handler and turned it into a folder action, without understanding the principle of the folder action.

I only commented on the fact that if nothing new is added to the hot folder or renamed inside it, the folder action will not run. Changing the contents of a previously added item does not trigger the folder action.

Indeed, the path to the added element should be determined automatically. If it’s a folder, you can filter its files in a repeat loop so that some are uploaded to the network and others are left alone.

If the OP wanted to edit the same added folder (or file) without changing its name and then upload it to the network some days, then he should have written a droplet instead of a folder action.