Script works when run, not so much for Folder Actions...

Is it me or is Folder Actions a bit… touchy?

My script below works perfectly well when run, but not when invoked by Folder Actions. I can’t tell exactly why, to be honest, but I think it has something to do with the path to the folder that invoked it. I’m wondering if anyone here knows how I could get this program working with Folder Actions?

The program is supposed to be activated when an image file is removed from, or added to, a designated folder. It would then scan that folder, a folder on a web server, and then either remove a file or add a file to the server. It then takes that list of image files from the local folder that has been uploaded, compiles some HTML, and updates the website’s gallery page to reflect the changes. Could be really neat, huh? Feel free to critique the script outside of the invocation, too, I’m sure it’s messy and all wrong!

Now, if only I could link this up to a progress bar and cancel button in xCode like the old days… without the Obj-C barrier! :lol:

Thanks in advance!

global localFolder, localFiles, remoteFolder, remoteFiles, theCode

--on adding folder items to localFolder
set localFolder to (POSIX path of "/Users/Username/Sites/Sitename/images/" as string)
--end adding folder items to

--on removing folder items from localFolder
--set localFolder to (POSIX path of localFolder as string)
--end removing folder items from

on setValues()
	set theCode to (do shell script "curl [url=]")[/url] as string -- gets main page source code as HTML template
	set localFolder to (POSIX path of localFolder as string) & "/"
	set remoteFolder to ("/" & (the last word of localFolder) & "/") -- gets parent folder of images
	set remoteFiles to (every paragraph of (do shell script "curl -s" & remoteFolder & "/ |  tail -n +3 | grep -o '[^ ]*$'")) -- gets a listing of all images in remote folder, each entry as an item in a list
	tell application "System Events" to set localFiles to (the name of every item of folder localFolder whose name does not start with ".") -- does the same as above but for the local directory, ignoring invisibles
end setValues

on swapFiles()
	repeat with lookUp in remoteFiles -- remove from server
		if lookUp is not in localFiles and (last word of lookUp) is in {"jpeg", "jpg", "png", "tiff"} then do shell script ("curl -s -Q \"DELE public/gfx/" & remoteFolder & lookUp & "\"")
	end repeat
	repeat with lookUp in localFiles -- add to server
		if lookUp is not in remoteFiles and (last word of lookUp) is in {"jpeg", "jpg", "png", "tiff"} then do shell script (("curl -T " & localFolder & lookUp & "" & remoteFolder & lookUp) as string)
	end repeat
end swapFiles

on swapCode()
	set imageCode to tab & tab & "<ul class=\"gallery\">" & («data utxt000A» as Unicode text) -- gallery uses <li> within a <ul> for each image
	repeat with imageFile in localFiles
		if (last word of imageFile) is in {"jpeg", "jpg", "png", "tiff"} then
			set imageCode to (imageCode & (tab & tab & tab) & "<li><img src=\"images/" & imageFile & "\" title=\"\" alt=\"\"></li>" & («data utxt000A» as Unicode text))
		end if
	end repeat
	writeOut((findReplace((text ((offset of "<ul class=\"gallery\">" in theCode) - 2) thru ((offset of "</ul>" in theCode) - 3)) of theCode, imageCode, theCode))) -- finds the area betwen the <ul> and </ul> in the code and replaces it with the <ul> and <li> with linsk to the images
end swapCode

on writeOut(theText)
	set theFileReference to open for access (localFolder & "/index.html") with write permission -- writes out locally, can't figure out a way to upload it to the server without writing it out first
	set eof of theFileReference to 0
	write theText to theFileReference starting at eof
	close access theFileReference
	do shell script "curl -T " & (localFolder & "/index.html") & "" -- uploads file to server
end writeOut

on findReplace(toFind, toReplace, theText)
	set astid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to toFind
	set textItems to theText's text items
	set AppleScript's text item delimiters to toReplace
	tell textItems to set editedText to beginning & toReplace & rest
	set AppleScript's text item delimiters to astid
	return editedText
end findReplace


folder actions don’t work on shared volumes

There aren’t any shared volumes used in this script, though. All internet-related activity is done through one-line cURL commands directly to variables in the script. The folder I use isn’t shared, or networked, just a regular folder with an action attached to it.

Probably the event handler works only with this syntax

on adding folder items to thisFolder after receiving theseItems
	-- do something
end adding folder items to