My Folder action deletes the folder action after completion

Hi, I’ve been cobbling together a Folder Action to print and delete files that arrive in a certain folder. I thought I had it cracked but each time it completes it seems to delete the folder action itself and so the next file doesn’t print until I’ve manually re-enabled Folder Actions. I’m not a profficient scripter and only just understand how this script works, but it seems that what I’m deleting is only the file that printed, so can anybody point out my mistake?.. please!

on adding folder items to thisFolder after receiving addedItems
	set myPrinter to "Deskjet_5900_series"
	set myFiles to list folder thisFolder without invisibles
	repeat with x from 1 to count myFiles
		set thisPath to POSIX path of file (thisFolder & (item x of myFiles) as string)
		set theShell to "lpr -P " & myPrinter & " " & thisPath
		try
			do shell script theShell
		on error err
		end try
		set theShell2 to "rm " & thisPath
		try
			do shell script theShell2
		end try
	end repeat
	delete addedItems
end adding folder items to

Model: Mac Dual 2.7 GHz PowerPC G5
AppleScript: 1.10.7
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

This might also work: (I’m unable to test this script.)

on adding folder items to thisFolder after receiving addedItems
	set printer to "Deskjet_5900_series"
	
	repeat with thisItem in addedItems
		try
			quoted form of POSIX path of thisItem
			do shell script "/usr/bin/lpr -P " & printer & " " & result & "; /bin/rm " & result
		on error errMsg number errNum
			display dialog "Error " & errNum & return & return & errMsg buttons {"Cancel Script", "Skip Item"} default button 2
			if (button returned of result) is "Cancel Script" then error number -128 -- cancel
		end try
	end repeat
end adding folder items to

Bruce thank you, I tested your script and at first thought it wasn’t working. It deleted the file but I got no printer output. On examining your shell script line it looked to me like you’d omitted the printer itself, so I added the printer into the code and bingo. Many thanks for your time on this, I will now make a colleague very jealous having gotten a solution by Monday.

Best regards and keep on scripting

Daz

Oops. :rolleyes:

Glad it worked for you after you fixed it. [Edited the script above.]