Stay-Open Script Hogging Processor Resources (Suggestions?)

Hello,

I have the following script set up to organize all of the Finder windows on my desktop and every time I go idle (I’m messy and always have a lot of windows open). The script is saved as a stay-open script and it runs itself every minute if I don’t touch my machine. Does anyone have any suggestions on how to improve my machine’s performance when running this script, or at least know why it takes up to 85% of my processor’s capacity when it executes while I’m idle?
I’m using a 1.25 GHz Power PC processor and I know that the other machine I use this script on is MUCH faster but it experiences the same problem.

Thank you!

Edit: My problem seems to only appear while I’m downloading a file. Thanks!


-- The trigger is used as a means to start the idle event
-- To change the time of the trigger simply replace the "1" with any other number, keep in mind that the idle timer is in
--    minutes
property triggerTime : 1 * minutes
-- On Idle means that once the system detects no activity from the user in the time specified by the trigger, the next
--    portion of the script will run
on idle
	-- The next line is determining how long the system has been idle from user interaction and stores that time as the 
	--    variable idleTime
	set idleTime to (do shell script "ioreg -c IOHIDSystem | perl -ane 'if (/Idle/) {$idle=(pop @F)/1000000000; print $idle,\"\";last}'") as integer
	--  If the amount of time in which the system has been idle is greater than the trigger time, the script executes
	--     the desktop organizer
	if idleTime > triggerTime then
		tell application "Finder"
			activate
			-- This next line sets the name of every new Finder window into a variable called windows
			set window_list to windows
			-- Setting the bounds and limits of the windows...
			set base_bounds to {10, 50, 300, 300}
			-- Setting the height and width increments between each newly placed Finder window
			set v_increment to 30
			set h_increment to 20
			-- Setting the variable window_list to reverse itself to ensure each Finder window was accuratel placed
			set window_list to reverse of window_list
			-- Ensuring that the placement process is repeated for each additional Finder window
			repeat with a_window in window_list
				-- Setting the view to list view
				set the current view of a_window to list view
				-- Setting up each Finder window's dimensions into a variable
				set bounds of a_window to base_bounds
				-- Setting each Finder window's position to be slightly more to the left and lower in appearance
				--    than the previous Finder window			
				set item 1 of base_bounds to (item 1 of base_bounds) + h_increment
				set item 3 of base_bounds to (item 3 of base_bounds) + h_increment
				set item 2 of base_bounds to (item 2 of base_bounds) + v_increment
				set item 4 of base_bounds to (item 4 of base_bounds) + v_increment
				-- Ending the repeat method that would run each Finder window through the script
			end repeat
			-- Ending the original Tell command
		end tell
		tell application "System Events"
			-- Perform a Command + Option + H to hide all visibile applications
			keystroke "h" using {command down, option down}
		end tell
		-- Setting the Finder windows to be visible
		tell application "System Events" to tell process "Finder" to set visible to true
	end if
	-- Return is taking the script back to its beginning to prepare for another possible script execution
	return 10
	-- End idle is ending the system's idle status and resuming an idle timer from the beginning 	
end idle



Tiger’s Finder is notoriously inefficient, and as long as you’re idle, why do you care how much CPU it uses – it uses a lot because you’re idle.

Because the amount of CPU the script uses sometimes causes other applications that I’m running to malfunction or my machine becomes locked-up. Would you recommend avoiding this script altogether given this information?

Thanks.

Just noticed that the problem you have occurs if the script fires in the middle of a download. I’m surprised downloading is considered “idle”. If your downloads are directed at a particular folder, you could test to see if that folder’s size was changing:

to folderReady(tFolder)
	set myFolder to tFolder as alias -- use the path to the folder that's loading
	set firstSize to size of (info for myFolder) --get initial size
	do shell script "sleep 3" -- wait 3 seconds (more efficient than delay)
	set newSize to size of (info for myFolder) --get a newer size, bigger
	repeat while newSize ≠ firstSize --if they don't equal, loop until they do
		set firstSize to newSize --new base size
		do shell script "sleep 3" --wait three seconds
		set newSize to size of (info for myFolder) --get a newer size
	end repeat --once the sizes match, the transfer is complete
	return true
end folderReady

Not that it would make a huge difference, but you can change the current view and bounds for all the Finder windows at the same time:

property triggerTime : 1 * minutes

on idle
	set idleTime to (do shell script "ioreg -c IOHIDSystem | perl -ane 'if (/Idle/) {$idle=(pop @F)/1000000000; print $idle,\"\";last}'") as integer
	
	if idleTime > triggerTime then
		set base_bounds to {10, 50, 300, 300}
		set v_increment to 30
		set h_increment to 20
		
		tell application "Finder"
			activate
			
			tell Finder windows
				set current view to list view
				set bounds to base_bounds
			end tell
			
			set window_list to reverse of (get Finder windows)
			
			repeat with a_window in window_list
				set item 1 of base_bounds to (item 1 of base_bounds) + h_increment
				set item 3 of base_bounds to (item 3 of base_bounds) + h_increment
				set item 2 of base_bounds to (item 2 of base_bounds) + v_increment
				set item 4 of base_bounds to (item 4 of base_bounds) + v_increment
			end repeat
		end tell
		
		tell application "System Events"
			keystroke "h" using {command down, option down}
			tell process "Finder" to set visible to true
		end tell
	end if
	
	return 10
end idle

Side note: windows in Finder could be any window (e.g. Get Info, Preferences, About); The Finder window class is the only window that has a current view property.

Adam - Yes they always go to a particular folder and that works perfectly thank you very much. Yeah I was surprised that downloading wasn’t considered “idle” either but your method takes care of that.

Bruce - Thanks for the shortcut :slight_smile:

There’s probably an obvious answer to what I ran into next, but if I have my machine automatically run my script on startup it seems to eat up all of my CPU as well. If I manually run the script it almost never takes any of my CPU. Does anyone have any advice?

Thanks,
Zach

I have a startup script application called “Delayed Start”:

tell application "Finder"
	set theDSfolder to alias ((path to home folder as text) & "Startup Delayed:")
	set lateStartApps to every item of theDSfolder
	delay 15
	repeat with k from 1 to count of lateStartApps
		open item k of lateStartApps
	end repeat
end tell

And in the top level of my Home folder, I have a folder “Startup Delayed” in which I place aliases to apps I want to have start after the machine gets going. You might try something like that. Your shell script is probably hung.