I sometimes have several duplicate Finder windows open at the same time. I wrote this AppleScript which quickly closes any duplicate windows, leaving only one of each open.
script pruneDupFWins
-- tested 07/09/12
-- Enhanced to consider Spotlight windows 09/09/12
-- Optimized for speed 10/09/12
-- © McUsr 2012 Parts made by Nigel Garvey
property parent : AppleScript
property scriptTitle : "Close Duplicate Windows"
property FinderIcon : a reference to file ((path to library folder from system domain as text) & "CoreServices:Finder.app:Contents:Resources:Finder.icns")
on run
local wCount, i, res, res2, fail, prevApp
script o
property wlist : {{}, {}}
property slist : {}
property klist : {}
property dlist : {}
end script
set fail to false
try
tell application id "com.apple.systemevents"
set prevApp to (name of every process whose frontmost is true and visible is true) as text
tell application process "Finder" to set o's slist to name of every window
end tell
tell application id "com.apple.finder" to activate
set sCount to count o's slist
if sCount = 0 then return 0
tell application id "com.apple.finder" to set o's wlist to {name, id} of (every window)
set wCount to count item 1 of o's wlist
set o's wlist to reverse of my transposeList(wCount, item 1 of o's wlist, item 2 of o's wlist)
set o's slist to reverse of o's slist
set i to 1
repeat sCount times
set res to my getSingelton(o's wlist, item i of o's slist)
if res = null then
else
if o's klist ≠ {} then
set res2 to my indexOfItem(item i of o's slist, o's klist)
if res2 = 0 then
set end of o's klist to item i of o's slist
else
set end of o's dlist to res
end if
else
set end of o's klist to item i of o's slist
end if
if i < wCount then set o's wlist to items 2 thru -1 of o's wlist
end if
set i to i + 1
end repeat
set wcl to count o's dlist
repeat with i from 1 to wcl
tell application id "com.apple.finder" to close Finder window id (item i of o's dlist as integer)
end repeat
on error e number n
-- Chris Stone
set {cr, sep} to {return, "------------------------------------------"}
set errmsg to sep & cr & "Error: " & e & cr & sep & cr & "Error
Number: " & n & cr & sep
tell application id "com.apple.systemuiserver"
activate
try
display dialog errmsg with title my scriptTitle buttons {"Ok"} default button 1
end try
end tell
set fail to true
set wcl to 0
end try
if not fail then
if wcl > 0 then
if wcl = 1 then
set msgText to "I closed " & wcl & " window!"
else
set msgText to "I closed " & wcl & " windows!"
end if
else
set msgText to "Nothing to do!"
end if
tell application id "com.apple.systemuiserver"
activate
try
display dialog msgText with title my scriptTitle buttons {"Ok"} default button 1 with icon my FinderIcon giving up after 1.2
end try
end tell
end if
tell application prevApp to activate
return wcl
end run
to transposeList(ctr, list1, list2)
-- tested 05/09/12
script o
property newL : {}
property m : list1
property n : list2
end script
local i
set i to 1
repeat ctr times
set end of o's newL to {contents of item i of o's m, contents of item i of o's n}
set i to i + 1
end repeat
return o's newL
end transposeList
on indexOfItem(theItem, itemsList) -- credit to Emmanuel Levy
local rs
set text item delimiters to return
set itemsList to return & itemsList & return
set text item delimiters to {""}
try
set rs to -1 + (count (paragraphs of (text 1 thru (offset of (return & theItem & return) in itemsList) of itemsList)))
on error
return 0
end try
rs
end indexOfItem
on getSingelton(the_list, item_a)
set astid to AppleScript's text item delimiters
-- Nigel Garvey's with a name change
set AppleScript's text item delimiters to return
set the_list_as_string to return & the_list & return
set AppleScript's text item delimiters to return & item_a & return
if (the_list_as_string contains result) then
set p to (count paragraphs of text item 1 of the_list_as_string)
if (p is 0) then set p to 1 -- Catch modern paragraph count for empty text.
set p to p mod 2
try
set otherItem to paragraph (p * 2 - 1) of text item (p + 1) of the_list_as_string
on error
return null
end try
set AppleScript's text item delimiters to astid
return otherItem
else
return null
end if
end getSingelton
end script
tell pruneDupFWins to run
Mac OS X Hints
http://hints.macworld.com/article.php?story=20120910081149969