Folder Action to FTP to Website

I am trying to write a folder action that will automatically upload iWeb files to my website after they are published to a folder from iWeb.

This is what I have so far. I know it is not working but I don’t know why. I also don’t know what I am doing.

on adding folder items to this_folder after receiving added_items
tell application “Finder” to repeat with this_item in added_items
simpleFtpUpload(“ftp://username:password@ftp.powweb.com/htdocs/”, this_item)
end repeat
end adding folder items to

Thanks for any help.

Hi,

the Finder is not able to do a FTP upload, but the shell is.
Try this

property serverPath : "[url=ftp://ftp.powweb.com/htdocs/]ftp.powweb.com/htdocs/[/url]"
property user_name : "username"
property pass_word : "¢¢¢¢¢¢"

on adding folder items to this_folder after receiving added_items
	repeat with this_item in added_items
		set itemPath to quoted form of POSIX path of this_item
		try
			do shell script "curl " & serverPath & " -u " & user_name & ":" & pass_word & " -T " & itemPath
		on error errmsg
			display dialog errmsg
		end try
	end repeat
end adding folder items to

iWeb makes an “index.html” file and a folder named “Site” in the folder that has the folder action attached. The script would upload the index.html file fine, but does not upload the folder “Site”. I read some about cURL and tried to make it work, but I have had no luck in getting the folder to upload. Any suggestions. Thanks.

Sorry, I forgot, that curl can only upload files, no folders.
You can either stuff (.tar) the whole thing and unstuff it on the Server or use a
scriptable FTP-client like Fetch, which is able to handle also directories.
Third option, if the folder structure doesn’t change, create it manually on the server and move file by file with curl

I ended up downloading Upload to FTP, an automator action and created an folder action script in automator with Get Folder Contents and Upload to FTP. It works, and although it won’t move the folder it does take all the files from the folder and moves them. That is nice because there is nothing for me to do. Thanks for your help. I am sure that I will utilize curl for something else.

[rant] Grrrr, I solved the curl folder problem well over bloody two hours ago, But wanted it to look in the sub folders of the “site” folder and do the same for them.

But it seems my hangover today is really having a effect on my scripting. because I for the life of me could not get the script to run a handler from its self, until i just realised I had named a variable the same bloody name. Over two hours that took me to spot that aggghhhhh. [/rant]

So here it is anyway, it works, I need to look at how it updates existing file or folder on the server. As I suspect it will ignore items if they already exist? EDIT seemed to have fixed that by touching the new folder"

Also did took out “/” that would have thrown the script

property serverPath : "[url=ftp://ftp.powweb.com/htdocs/]ftp.powweb.com/htdocs/[/url]"
property user_name : "username"
property pass_word : "¢¢¢¢¢¢"


on adding folder items to this_folder after receiving added_items
	--set added_items to ("Macintosh HD:Users:userName:Documents:" & "site" as string) as list --for testing
	repeat with this_item in added_items
set this_i to (this_item as alias) as string
	set t to quoted form of POSIX path of this_i
	set touch to do shell script "touch " & t
		tell application "Finder"
			set this_item to this_item as alias
			log this_item
			set this_class to kind of this_item
			log this_class
			if this_class is "Folder" then
				my sub_item_s(this_item)
			else
				set itemPath to quoted form of POSIX path of this_item
				my ftp(itemPath)
			end if
		end tell
	end repeat
end adding folder items to
on sub_item_s(this_item)
	tell application "Finder"
		set serverPath to serverPath & name of this_item & "/" as string
		set sub_items to (items of this_item)
		
		if sub_items is not {} then
			repeat with sub_item in sub_items
				set this_item to sub_item as alias
				set this_class to kind of this_item
				if this_class is "folder" then
					my sub_item_s(this_item)
				else
					set itemPath to quoted form of POSIX path of this_item
					my ftp(itemPath)
				end if
			end repeat
			
		end if
	end tell
end sub_item_s

on ftp(itemPath)
	do shell script "curl --ftp-create-dirs " & serverPath & " -u " & user_name & ":" & pass_word & " -T " & itemPath
end ftp



Hi Mark,

very good catch :slight_smile:

Some suggestions/notes:
¢ The default setting of curl is to overwrite files.
¢ Checking the kind property of files/folders will fail on non-english systems, because the localized name of “Folder” is displayed
¢ An additional local variable for the server path is needed, using only a property adds every new directory level to the path but never removes it
¢ The Finder is only necessary to list the items of a folder
¢ the parameters of a folder action handler are always an alias or alias list

property serverPath : "[url=ftp://ftp.powweb.com/htdocs/]ftp.powweb.com/htdocs/[/url]"
property user_name : "username"
property pass_word : "¢¢¢¢¢¢"

on adding folder items to this_folder after receiving added_items
	repeat with this_item in added_items 
		if folder of (info for this_item) then
			sub_item_s(serverPath, this_item)
		else
			ftp(serverPath, POSIX path of this_item)
		end if
	end repeat
end adding folder items to

on sub_item_s(subPath, this_item)
	set subPath to subPath & name of (info for this_item) & "/"
	tell application "Finder" to set sub_items to (items of this_item)
	repeat with sub_item in sub_items
		if folder of (info for sub_item as alias) then
			sub_item_s(subPath, sub_item as alias)
		else
			display dialog subPath
			ftp(subPath, POSIX path of (sub_item as alias))
		end if
	end repeat
end sub_item_s

on ftp(sPath, iPath)
	do shell script "curl --ftp-create-dirs " & quoted form of sPath & " -u " & user_name & ":" & pass_word & " -T " & quoted form of iPath
end ftp

Hey Stefan, Nice one.

I can not believe the trouble I had getting my brain working yesterday. :rolleyes:

Thanks

I added --anyauth

Hopefully works and stops the password being sent in clear text.

on ftp(sPath, iPath)
	do shell script "curl --ftp-create-dirs " & quoted form of sPath & " --anyauth -u " & user_name & ":" & pass_word & " -T " & quoted form of iPath
end ftp

just for fun a optimized version using the shell find command.
It copies also invisible files except DS-Store files or no invisible files at all (with -type f ! -name ‘.*’)

property serverPath : "[url=ftp://ftp.powweb.com/htdocs/]ftp.powweb.com/htdocs/[/url]"
property user_name : "username"
property pass_word : "¢¢¢¢¢¢"

on adding folder items to this_folder after receiving added_items
	set {TID, text item delimiters} to {text item delimiters, "//"}
	repeat with this_item in added_items
		if folder of (info for this_item) then
			set these_items to paragraphs of (do shell script "find " & quoted form of POSIX path of this_folder & " -type f  ! -name '.DS_Store'") -- or  ! -name '.*'")
			repeat with i in these_items
				ftp(serverPath & text item 2 of i, i)
			end repeat
		else
			ftp(serverPath, POSIX path of this_item)
		end if
	end repeat
	set text item delimiters to TID
end adding folder items to

on ftp(sPath, iPath)
	do shell script "curl --ftp-create-dirs " & quoted form of sPath & " --anyauth -u " & user_name & ":" & pass_word & " -T " & quoted form of iPath
end ftp

This is fantastic. I decided to use this to ftp photos the the gallery page of the website. So I export from Lightroom to a folder and then the folder action script takes the new folder and ftp’s to the same folder. Because it takes some time for lightroom to export the photos, at first it would only create the folder and send the first photo exported. I change the first of the script to

property serverPath : "[url=ftp://ftp.powweb.com/htdocs/photos/albums/]ftp.powweb.com/htdocs/photos/albums/[/url]"
property user_name : "username"
property pass_word : "password"

on adding folder items to this_folder after receiving added_items
	delay 300
	repeat with this_item in added_items

Now it waits for all the photos to export.

However, it is asking me for every photo if I want to ftp. How can I get rid of the dialog and just have it send every photo without any interaction with me. Thanks. I can’t say thanks enough, as all of this is way beyond my abilities.

Todd

Sending you an email. NOW

Thanks.

The optimized version works fantastic. Thanks for the assistance.