Close Duplicate finder windows
Macosxhints Posted a Applescript hint on Macosxhints to close duplicate finder windows.
The idea of the script is sometimes you may find you have multiple finder windows open. Many of them leading to the same target (folder). And want a quick way of closing all the duplicate windows.
But I couldn’t understand why it was so long and complicated. So as an excise for myself wrote one that did a similar job
Complexity
The complexity of the script I think comes from the Poster trying to compare open windows by name and creating two lists and then comparing them.
Update. ( The poster has explained that the complexity is in fact due to their script only closing windows in the current space and to trying to get the most speed when the script runs.
It turns out that system events will only return the windows of the current space when asked to ask the application process “Finder” to get it’s windows. Where asking the application “Finder” to do so will return all windows in all spaces.
Although closing windows in the current space works for some. It does not for me. I want all duplicate windows closed. Also for a script such as this I do not need it to be optimised for speed. I will never have tons of duplicate window open, just a few which will close fast enough.)
Names of variables.
The original script’s code is full of short variable names that do not help in reading the flow of the script. i.e wlist, slist. ???. partly why I was not sure why it is so complex :-).
I used to do this with my names of variables in my code. But all too often someone would point out to me that names of variables should in practice help the reader of the code understand what it is for.
i.e theWindowList, windowID, windowName.
Its easy to slip into the bad habit of writing short names to avoid loads of typing and you think you will remember whats what later on. But trust me you don’t. Its harder at a later stage to follow the logic of the script and the logic of your thinking. And I especially try and practice when posting scripts out there for others to go over. (although I a sure I slipped once or twice)
Windows with the same name but different Paths.
The original script used a unix command entered into the Terminal.app to enable the posix-path-name to be displayed in the windows title bar.
$ defaults write com.apple.finder _FXShowPosixPathInTitle -bool YES
They then could use the posix-path-name in the title bar i.e /Users/UserName//library to make sure that directories with the same name but different paths would not be mixed up.
Not taking this into account and checking for windows with matching names but different paths could mean windows with the same name but different paths being closed by mistake.

Using the unix command and having long posix-path-names in the title bar is a not for me. Especially as I often have windows open that go deep down into the directory levels. Combined with keeping the window width small the path-name gets truncated to the point of useless.
Get a Target.
In this script I will be using target of window.
target (specifier) : the container at which this file viewer is targeted
This is basically the windows target folder/directory path.
tell application “Finder” to set theWindowTarget to target of window 1
will return : folder “Library” of startup disk of application “Finder”
Having the target is one of the best ways to compare or tell which window is which, especially if they have the same name.
My revised script takes in to account of the above by checking the target of each window.
- It places the POSIX path of the target into the keepWindowList .
- Then gets and checks if the next window target is already in the keepWindowList.
- If it is in the keepWindowList it adds 1 to the closeWindowCount and then closes that window straight away.
- Windows of the target items put in the keepWindowList will not be closed.
- The closeWindowCount is used at in the end dialogue.
Now saying all that.
There is one issue when looking for target of open Finder windows.Spotlight search windows and Computer Container windows will not return a target.
A Computer Container window:
tell application “Finder” to set theWindowTarget to target of window 1
will return :computer container of application “Finder”
A Spotlight Search window:
tell application “Finder” to set theWindowTarget to target of window 1
will return :alias file “” of application “Finder”
Trying to do stuff on these returns will lead to errors. So these are where I
will use the name of the window.There is only ever going to be one
Computer Container for your mac.
And I have found no way of distinguishing
target paths of Search windows.Because of this the
Names will be added to the same
keepWindowList as the real
targets.
Since the Spotlight Search windows can be searching two different windows with the same name, you can alter the script to just ignore Spotlight Search windows but I have not done so here.
[iframe /blogR/closeFinderWindowsCodeNEW.html 700 1210]
Image gallery: