how can i make this code more reliable

hi all

I have been using the below code now for along time, it sits in the background checking a folder “TRANSFER” and as
.ps (Post Script) files enter it, it checks for the .indd.ps extension, if .indd is there it removes it and then moves the file to a external drive replacing any existing files that match the same name, else if no .indd extension exists, it moves the .ps file to external drive replacing file if it already exists, works great and has done for ages.

I am now putting through heaps more .ps files and the script has become quite slow, even quitting if it cant cope,
so i’m looking for something that is quick ans stable and was thinking shell script maybe, problem is i don’t no shell code well at all, any chance someone could help out here please.

OSX 10.8.4

--on run
--Any initialization that needs to happen - mount servers? launch apps?
--end run

on idle
	set PathToDesktop to "RIP FILES:TRANSFER:" as text
	set this_folder to (PathToDesktop) as alias
	tell application "Finder" to set these_items to (get files of entire contents of this_folder whose name ends with ".indd.ps")
	if the number of items in these_items is greater than 0 then
		tell me to activate
		tell application "Finder" to repeat with f in (get files of entire contents of this_folder whose name ends with ".indd.ps")
			set f's name to text 1 thru -8 of (get f's name) & "ps"
			set _path to quoted form of POSIX path of "RIP FILES:"
			repeat with i from 1 to number of items in these_items
				move items of this_folder to folder "RIP FILES:" with replacing
			end repeat
		end repeat
	else
		tell application "Finder"
			move items of this_folder to folder "RIP FILES:" with replacing
		end tell
	end if
	return 5
end idle

The problem, mostly, by slowing down stay open AppleScript is that it has memory leaks in it. So after a while it will not only slow down the script but your entire machine as well. When people use an idle handler I always advise to them that they use some sort of idle counter or something so it will start a new instance of itself (a new process) and quit the current one. I see you repeat the script after 5 seconds, so let’s say you have an counter that will launch a new instance of itself after 1440 runs (approx. 2 hours).

something like:

property idleCounter : missing value

on run argv
	set idleCounter to 0
end run

on idle
	set idleCounter to idleCounter + 1
	--insert your code here
	if idleCounter > 1440 then
		do shell script "open -n " & quoted form of POSIX path of (path to me)
		quit
	end if
	return 5
end idle

on quit
	continue quit
end quit

edit:

to answer your bash question. A similar idle handler in bash will be:

The sleep command uses unix sleep call; it doesn’t consume CPU usage.

As well as what DJ says, your code needs rationalising. Asking for the entire contents of a folder is something that takes time; you’re inexplicably doing it twice.

Hi. I noticed the same duplicated command as Shane. You also say you’re depositing “heaps” of files into the transfer folder; how many files constitute a heap”100, 1000, 10,000? Are said files nested in folders? If not, you probably don’t even need entire contents, which becomes unreliable with large quantities.