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
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 applicationprocess “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.
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 targetofwindow.
target (specifier) : the container at which this file viewer is targeted
This is basically the windows target folder/directory path.
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.