System Events eating my CPU cycles

Occasionally I will have the problem where the System Events process constantly uses 65-100% of my CPU cycles, effectively crippling my machine. I traced the problem to having folder actions enabled, and then to a specific script. Because I use folder actions on mounted shares, I use a script to first mount all the network drives I use, and then delete all my folder actions and re-associate them with the correct folders. I have 4 actions that I use. 3 are on network folders and one on my local machine. I traced the problem by adding only 1 action at a time, and it turns out it is actually the action on a local folder that is the problem. The script monitors my HD:private:var:tmp:folders.501:TemporaryItems folder for temporary Quark files, and tells Quark to set the bounds to fit my screen layout. The script is below. Does anyone know why it would suddenly be chewing through my CPU like this? I should note that for about 90% of the time I’ve had this action installed, it has worked flawlessly without effecting CPU usage. Occasionally it would start using all my CPU, at which point I would delete the and files, which would return my CPU to proper levels. Except this last time, deleting plist files doesn’t make a difference. As soon as the action is added again, CPU jumps back up.

on adding folder items to this_folder after receiving added_items
	repeat with i in added_items
		tell application "Finder" to if (name of i) contains ".qxp" then
			tell application "QuarkXPress"
				set bounds of every document to {22, 32, 916, 1180}
				set view scale of every document to fit page in window
			end tell
		end if
	end repeat
end adding folder items to

Model: PM G5
Browser: Firefox 3.6.16
Operating System: Mac OS X (10.4)

I don’t know quark express, so I can’t confirm the correctness of the code. But assuming it is correct, the problem I see is your nested “Tell application” statements. I always tell people do not nest these if you can avoid it. In essence you are telling the Finder to tell quark to do something. Obviously you don’t need the Finder to tell quark, so why write your code that way? It’s inefficient and thus could cause issues. So un-nest your application tell blocks and see if that fixes your problem. For example your repeat loop could be this…

set runQuarkCode to false
tell app “Finder”
if (name of i) contains “.qxp” then set runQuarkCode to true
end tell

if runQuarkCode then
tell application “QuarkXPress”
set bounds of every document to {22, 32, 916, 1180}
set view scale of every document to fit page in window
end tell
end if

Thanks! That seems to have tamed the beast, for the moment anyway. I guess only time will tell if it is a permanent fix.

Hi, Hank.

In the current situation, this can of course be more elegantly and efficiently rendered as:

tell application "Finder" to set runQuarkCode to ((name of i) contains ".qxp")

Ah, I like that Nigel. Good suggestion.