Can I make a script run whenever I open a Finder window?

Because I hate the icon spacing in Tiger I have written the following script to reorder them a bit closer together.


set cx to 1
set ypos to 33

tell application "Finder"
	repeat with i from 1 to (number of items in front window)
		if cx = 4 then
			set cx to 1
			set ypos to ypos + 70
		end if
		set xpos to -50 + (cx * 100)
		set position of item i of front window to {xpos, ypos}
		set cx to cx + 1
	end repeat
end tell

Is it possible to make this run when I open any finder window?

Hi Mark,

There are two ways that I can think of to do this and both are not good. An alternative would be to run a script that opens the folder.

Of the first two methods, running a stay open script with an idle handler is the best. Your idle handle will monitor the opening of Finder windows. The second method was really bad because I already forgot what it was. :slight_smile:

gl,

Something like this? It seems to work ok and only uses 0.8% of the CPU.


set f to ""

repeat
	tell application "Finder" to set w to the number of windows
	if w is not 0 then
		tell application "Finder"
			if the name of the front window is not f then
				set colcount to 1
				set ypos to 33
				set f to the name of the front window
				repeat with i from 1 to (number of items in front window)
					if colcount = 4 then
						set colcount to 1
						set ypos to ypos + 70
					end if
					set xpos to -50 + (colcount * 100)
					set position of item i of front window to {xpos, ypos}
					set colcount to colcount + 1
				end repeat
			end if
		end tell
	end if
	delay 1
end repeat

You’d use even fewer CPU cycles if you used an idle handler in place of a repeat loop containing a delay, Mark.

The following example acts on all newly opened windows, whether or not they are frontmost. However, once a window has been cleaned up, the script should then effectively ignore it - for as long as it remains open.

(This approach should allow the user to temporarily move items around in a window - without the script constantly chipping in and trying to move them back again. In other words, any further cleaning up is deferred until the window is closed and then reopened.)

Save as a stay-open application:

property idList : {}

on idle
	tell application "Finder" to try
		tell (get windows whose id is not in idList)
			if (count) is 0 then return 1 (* seconds before next check *)
			repeat with w in it
				tell (get w's items) to repeat with i from 1 to count
					tell i - 1 to set p to {it mod 3 * 100 + 50, it div 3 * 70 + 33}
					tell item i to if position is not p then set position to p
				end repeat
			end repeat
		end tell
		set idList to id of windows
	end try
	1 (* seconds before next check *)
end idle

on quit
	set idList to {}
	continue quit
end quit

tell application "System Events" to set visible of first process whose frontmost is true to false

I think the other method you were thinking of was Folder Actions. It would be bad because you’d potentially have to enable it for all folders on your hard drive.

:slight_smile:

Yes I thought of using Folder actions but as you said I would have to enable it for every single folder. Thanks for the code Kai, I have used it in my programme and it’s working great.

Cheers
Mark

You’re most welcome, Mark. And thanks for the feedback - it’s always useful to know whether or not a suggestion has done the trick. :slight_smile: