Hello!
This is not an optimized version, nor a totally correct one, as I think my assumption about the windows being retrieved in random order, or not by index to be wrong. I’ll come back with a slightly faster solution.
Edit: Not faster, but more robust, as it now considers info-windows, but not the preferences window, I have no clue whether that is necessary or not.
Edit
I’m coming back to this one later if nobody beats me to it. I will bail out if there is any folder prefernces windows open, leave clippings windows be, and the same goes for finders preference’s window.
Having said that:
This is a script to be run to keep order on your own desktop, I haven’t used clippings windows in a long time, but those must be considered.
But for the other settings windows; info windows, preferences windows for folders, and finders preverences window, I tend to close those, as soon as I am finished with them.
But usage patterns may vary.
The main objective with this script anyway, was to show a technique to deal with windows just in your own space.
That system events are smart enough to only return the windows from the current space of an an application process or process whose visible is true.
Edit II
The code below works because I have configured Finder to show the posix paths in the title bar I think
You have to enter this in your terminal window, it should work for Leopard and later, but I used to have it this way
in Tiger as well.
defaults write com.apple.finder _FXShowPosixPathInTitle -bool YES
killall Finder
You can read more about that here:http://hints.macworld.com/article.php?story=20071101210524604
Enjoy
” © Mcusr and Put in Public Domain.
” If use, or you derive the principles from this code, and posts elsewhere, or uses it in something you
” get paid or kudos for, then you agree to refer to this post by using the code found here, or the
” underlying principles! :)
set {ix, forSave} to {2, 2}
set {spaceWins, spaceWNames} to {{}, {}}
-- we fetch references to its finder windows in THIS SPACE
-- This is apliccable to other applicatons
-- How to get the positon, may vary, but the logic, or lack of, stands!
tell application "System Events" to tell process "Finder"
set wc to (get count its every window)
repeat with i from 1 to wc
copy (a reference to its window i) to end of spaceWins
copy (get name of its window i) to end of spaceWNames
end repeat
end tell
set spaceCT to (get count spaceWins)
if spaceCT < forSave then error "not enough Finder windows in this space"
-- Considers info windows.
-- The trick about info windows is that they contains "info" in their name
-- ugly .. I know, but I can't get the class out of system events.
-- if those windows are laying inside the range, then the index has to be
-- skewed backwards, as the info windows are not considered.
-- I don't dare closing them, after having closed finder windows,
-- which is the job I am to do.
-- Finder windows must necessarily by laying after any info windows
-- in the z-layering, with higher indicies.
-- Skews the index, ensuring enough Finder windows will be saved.
set toSkew to 0
set tix to 1
repeat with awnName in spaceWNames
if tix < (ix + toSkew) then
if contents of awnName contains "-info" then
set toSkew to toSkew + 1
else
set tix to tix + 1
end if
else
exit repeat
end if
end repeat
-- Having found the skew factor, we add it to the
-- index of the layered windows.
set ix to ix + toSkew
-- Finds every window we want to keep, they may be in
-- every SPACE
set keepers to {}
tell application "Finder"
set keepers to (get its every Finder window whose index < (ix + 1)) as list
set keepCt to (get count keepers)
if keepCt < forSave then error "not enough Finder windows in this space"
-- We prune out the windows from the list we obtained by
-- System Events, as we are to spare those.
set toDel to spaceCT
repeat with i from 1 to spaceCT
copy position of (contents of item i of spaceWins) to sPos
repeat with j from 1 to keepCt
copy position of (contents of item j of keepers) to kPos
if item 1 of sPos is equal to item 1 of kPos and (((item 2 of sPos) + 22) is equal to item 2 of kPos)
then
” We identify the windows by their position, as many Finderwindows can have the same name
” And we haven't got an Id of it, so this is the way. If the windows are positioned in EXACTLY
” same place on another space, then this goes wrong, but the probabilty for that is hopefully
” low, - or this script isn't for you! :)
set item j of spaceWins to missing value
set toDel to toDel - 1
exit repeat
end if
end repeat
end repeat
-- Purges the Finderwindows we want to get rid of.
set i to 0
repeat with awinInSpace in spaceWins
set i to i + 1
if contents of awinInSpace is not missing value then
repeat with aFwin in (get every Finder window)
if name of contents of aFwin is contents of item i of spaceWNames and index of contents of aFwin > ix then
set amatch to Finder window id (id of contents of aFwin)
close amatch
set toDel to toDel - 1
exit repeat
end if
if toDel is 0 then exit repeat
end repeat
end if
end repeat
-- if there were other windows like info windows, then they would be spared
end tell