How to make "on idle" work?

I’m trying to make this script run in the background and upload files to a server when they’re added to a folder. Unfortunately it just ends without doing anything at all, so I must be doing the “on idle” stuff wrong. If I remove it, it works pretty much the way I want it to. Can anyone help me figure out what I’m doing wrong? There must be some fundamental thing I’m missing in my script.

global theWatchedFolder

on run
	set theWatchedFolder to "Mac HD:Users:pp:Main:jlist:upload" as alias
	set thefolder to "f5"
	
end run

on idle
	tell application "Transmit"
		set thedocs to every document
	end tell
	
	
	tell application "Finder"
		set theDetectedItems to every item of theWatchedFolder
		if length of theDetectedItems is not 0 then
			tell application "Transmit"
				set theWindow to name of window 0
			end tell
			if theWindow = "images.jlist.com - html" or theWindow = ("images.jlist.com - " & thefolder) then
				-- do nothing, already open
			else
				-- starting the new script
				-- opening connection with Transit
				set theServerAddress to "aserver.com"
				set theUserName to "images"
				set thePassword to "-------"
				set theDirectory to "html/"
				tell application "Transmit"
					set theDocument to make new document with properties {name:theServerAddress}
					tell theDocument
						tell current session
							connect to theServerAddress as user theUserName with password thePassword with initial path theDirectory
						end tell
					end tell
				end tell
			end if
			
			repeat with aDetectedItem in theDetectedItems
				set thename to name of aDetectedItem
				
				set file1 to "Mac HD:Users:pp:Main:jlist:upload:" & thename as alias
				
				tell application "Transmit"
					tell document 1
						tell current session
							set their stuff to "/html/" & thefolder & "/"
							upload item file1 with resume mode replace
						end tell
					end tell
				end tell
				
			end repeat
			tell application "Transmit"
				tell document 1
					close
				end tell
			end tell
			
		end if
	end tell
	
	
	return 5
end idle

Model: Mac Pro
AppleScript: 2.2
Browser: Safari 523.10
Operating System: Mac OS X (10.5)

idle should only work for scripts saved as a stay-open application.

See also: idle and quit Handlers for Stay-Open Applications

The “standard” on idle script, saved as a stay-open application, looks like this:

on run
	(*do setup - this runs when the application is started to set things up. It is not necessary to have an 'on run' handler because the on idle handler runs immediately after this anyway. If you want this to run all the time after every login, list it your startup items. You quit it from it's dock icon.*)
end run

(*'on idle' runs immediately following the 'run' handler if there is one, and thereafter on the interval specified in the return at the end. (An 'on run' handler is not required, so omit if there is no setup)*)
on idle
	-- In this section, you do your script operations every interval
	return xx -- do this every xx *seconds*.
end idle

on quit
	(*do stuff - assumes you want to clean things up or save preferences or change properties before the program quits. This section runs only if the application is quit from the dock, or by any other means. It is not necessary to have one.*)
	
	(*if there is an on quit handler, then 'continue quit' must be the last statement or the script will not stop! The presence of an 'on quit' handler 'grabs' the system command to stop, so it will not complete the quit process until notified to do so. 'continue quit' does that.*)
	continue quit
end quit

Thanks for the help! I also figured out that it had to be running as an application and staying open.