Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Close Duplicate Finder windows Apps
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

[kirkmc adds: Looking at the script, I really don't understand much, but I tested it and it works.]
    •    
  • Currently 3.40 / 5
  You rated: 5 / 5 (10 votes cast)
 
[3,767 views]  

Close Duplicate Finder windows | 13 comments | Create New Account
Click here to return to the 'Close Duplicate Finder windows' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Close Duplicate Finder windows
Authored by: MacUser06 on Sep 27, '12 07:37:35AM

One out of 10 times, the script crashes for reasons I cannot fathom. Then I close a window I have two of, and reruns the script. I usually have above 20 open finder windows, when I use this script, to get the numbers down.

It doesn't change the order of windows, so you can still use the z-layer of the windows as a history about where you have been.



[ Reply to This | # ]
Close Duplicate Finder windows
Authored by: llee on Sep 27, '12 08:15:06AM

I haven't made the script crash yet, but this is the kind of computer solution I like the most, I think. In former versions of Mac OS X you could press cmd-ctl-up arrow to open the parent of the current folder, then in the new window double-click then icon of the folder you were originally viewing, and the windows would reverse order. Now (Mtn. Lion) when you do that you just get a duplicate window. In other words, it's much easier to get duplicate windows now than it used to be. The fact that duplicate windows can exist at all represents a serious deprecation of the Mac OS since the classic Mac system, IMO. Of course, few people really care about that these days, I guess.



[ Reply to This | # ]
Close Duplicate Finder windows
Authored by: Ronald Lanham on Sep 27, '12 10:55:59AM
"In former versions of Mac OS X you could press cmd-ctl-up arrow to open the parent of the current folder, then in the new window double-click then icon of the folder you were originally viewing, and the windows would reverse order. Now (Mtn. Lion) when you do that you just get a duplicate window."
llee... I can still do that in OS X 10.8.2

However... I often end up with duplicate Finder windows opened that makes it confusing... so I hope this script works.

If it works (I haven't tried it yet) kudos to the author.

Hard to believe Apple hasn't fixed this in the many years it's existed.
Edited on Sep 27, '12 11:01:26AM by Ronald Lanham


[ Reply to This | # ]
Close Duplicate Finder windows
Authored by: Ronald Lanham on Sep 27, '12 12:34:16PM

You can test if this script is working correctly (which it does for me)...

First make sure you have the Path Bar displayed. (menubar > View > Show Path Bar)

Then simply Command-(double-click) on the folder icon in the Path Bar of the Finder window you want to duplicate (again... this is for testing the script)... and a duplicate Finder window will appear.

Now run the script. You should now see that the duplicate Finder window disappears.

I used the Apple Script Editor to save the script as an app and then dragged the app's icon to the Finder window toolbar.

Now it's easy to simply click on the "Close Duplicate Finder Windows" app to have any duplicates disappear.

Thanks Tommy for the script! Much appreciated.

Edited on Sep 27, '12 12:36:58PM by Ronald Lanham



[ Reply to This | # ]
Close Duplicate Finder windows
Authored by: Anonymous on Sep 27, '12 01:46:17PM

I don't really get why you would want to close duplicate windows. I mean, you might have two views open on the same directory: one the plain grid view, the other a list-tree view. Would this script close one of those two? Does it close the elder, or the younger? Or is it random?

And I also don't get why people seem to think Mac OS X allowing duplicate windows is somehow a bug. If you want limitations, get a Midnight Commander clone.



[ Reply to This | # ]
Close Duplicate Finder windows
Authored by: MacUser06 on Sep 28, '12 03:18:45AM

Hello!

This script would never have been that fast, hadn't it been for the handler of Nigel Garvey.

I have *maybe* figured out the bug, which I think is the fact that spotlight windows doesn't have a name. I think I initially thought, it was better to get a run time error with an even or uneven number spotlight windows, for reasons I don't want to go in to.

I'm trying really hard to find a way to fix this, it is either the run time error, or to close Spotlight windows. I prefer to keep my Spotlight windows open!

Edited on Sep 28, '12 03:39:09AM by MacUser06



[ Reply to This | # ]
Close Duplicate Finder windows
Authored by: MacUser06 on Sep 28, '12 05:51:08AM
The bug is removed for good. I fathomed what it was in the end. The script should work flawlessly for every foreseeable situation. 覧覧覧覧覧覧覧覧覧覧覧覧覧

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
	-- 28/09/12 *Totally* Debugged and improved.
	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)
			
			repeat with i from 1 to count o's slist
				if item i of o's slist = missing value then set item i of o's slist to "_mcusr_"
			end repeat
			
			repeat with i from 1 to length of item 1 of o's wlist
				if item i of item 1 of o's wlist = "" then set item i of item 1 of o's wlist to "slight"
			end repeat
			
			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 # Spotlight windows goes here!
				else
					if o's klist ≠ {} then
						set res2 to my indexOfItemN(item i of o's slist, o's klist, 1)
						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

					set j to indexOfItemN(item i of o's slist, o's wlist, 2)
					if j = 1 then
						set o's wlist to rest of o's wlist
					else if j  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 indexOfItemN(item_a, the_list, n)
		local astid, p
		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)
			set AppleScript's text item delimiters to astid
			if (p is 0) then return 1 -- Catch modern paragraph count for empty text
			return (p div n + p mod n)
		else
			set AppleScript's text item delimiters to astid
			return 0
		end if
	end indexOfItemN
	
	
	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
-----------------------------------------------------------------------------
Edited on Sep 28, '12 06:03:28AM by MacUser06


[ Reply to This | # ]
Close Duplicate Finder windows
Authored by: mark hunte on Sep 29, '12 03:35:37AM

This code is broken

---
mh



[ Reply to This | # ]
Close Duplicate Finder windows
Authored by: Strod on Oct 03, '12 09:21:25AM

@MacUser 06, I believe you pasted the wrong version of your "definitive" script. The line:

else if j 0 then

doesn't make much sense and in fact the whole repeat sCount times section looks weird (as it has an else and is closed by end if.

Any chance you could give us the right version? Anyway, thanks for the original version. I will try to study both versions and perhaps figure out how to correct the new one.



[ Reply to This | # ]
Close Duplicate Finder windows
Authored by: MacUser06 on Oct 21, '12 10:28:12PM
I am sorry for that, and I don't know how that happend, you were indeed right. Here is the correct version:

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
	-- 28/09/12 *Totally* Debugged and improved.
	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)
			
			repeat with i from 1 to count o's slist
				if item i of o's slist = missing value then set item i of o's slist to "_mcusr_"
			end repeat
			
			repeat with i from 1 to length of item 1 of o's wlist
				if item i of item 1 of o's wlist = "" then set item i of item 1 of o's wlist to "slight"
			end repeat
			
			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 # Spotlight windows goes here!
				else
					if o's klist ≠ {} then
						set res2 to my indexOfItemN(item i of o's slist, o's klist, 1)
						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
					
					set j to indexOfItemN(item i of o's slist, o's wlist, 2)
					if j = 1 then
						set o's wlist to rest of o's wlist
					else if j < length of o's wlist then
						set o's wlist to items 1 thru (j - 1) of o's wlist & items (j + 1) thru -1 of o's wlist
					else
						set o's wlist to items 1 thru -2 of o's wlist
					end if
				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 indexOfItemN(item_a, the_list, n)
		local astid, p
		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)
			set AppleScript's text item delimiters to astid
			if (p is 0) then return 1 -- Catch modern paragraph count for empty text
			return (p div n + p mod n)
		else
			set AppleScript's text item delimiters to astid
			return 0
		end if
	end indexOfItemN
	
	
	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


[ Reply to This | # ]
Close Duplicate Finder windows
Authored by: mark hunte on Sep 29, '12 03:31:04AM
Hmm, this seems a lot of script to just close some windows. Also if I have two windows named for example "Library" open but both lead to two different paths i.e users Library and system Library. Then one of them will get closed by mistake.

This code is less and actually looks at the targets (path) of the windows before closing them. There is a simple spotlight window check, not as robust as I would like but time is money..:-)

set keepWindowList to {}
set closeWindowCount to 0
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")
tell application "Finder"
	set theWindowList to every Finder window
	repeat with i from 1 to number of items in theWindowList
		set this_item to item i of theWindowList
		set windowID to id of this_item
		set windowTarget to target of this_item
		set windowName to name of this_item
		log windowName
		if windowName does not contain "Searching " then
			set windowNTargets to POSIX path of (windowTarget as alias)
			
			
			if windowNTargets is in keepWindowList then
				set closeWindowCount to closeWindowCount + 1
				close this_item
			else
				copy windowNTargets to end of keepWindowList
			end if
			
		end if
	end repeat
	
end tell

if closeWindowCount > 0 then
	if closeWindowCount = 1 then
		set msgText to "I closed " & closeWindowCount & " window!"
	else
		set msgText to "I closed " & closeWindowCount & " 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
---
mh
---
mh
Edited on Sep 29, '12 03:33:26AM by mark hunte


[ Reply to This | # ]
Close Duplicate Finder windows
Authored by: MacUser06 on Oct 21, '12 10:21:07PM

Hello!

The script is intended to work with pathnames in the finder windows, alleviating the need to use the target, at the same time, System Events will just return the windows of the current space.

One shouldn't mess with windows in other spaces. It also preserves the window layering order, so that you can backtrack where you have been by the order of the finder windows.

You can see how to set the pathname as window name by reading this post:

show-full-directory-path-in-finder-window-title-bars

[ Reply to This | # ]
Close Duplicate Finder windows
Authored by: MacUser06 on Nov 15, '12 04:36:48AM
This is the definitive version, that works with both Posix Paths and folder titles, it considers Computer windows, and Spotlight windows.


script pruneDupFWins
	-- ゥ MacUser06/McUsr 2012 28.10.2012 completely rewritten to cope with all cases of window titles, and computer windows
	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")
	
	# COLLECTING NECESSARY DATA
	# fastest way to make SystemEvents, get the window count for the windows appearing in the current space.
	local fwc, clcount, prevApp, pathInTitles # Finder Window Count, close count
	set pathInTitles to false
	try
		set pathInTitles to (do shell script "defaults read com.apple.finder _FXShowPosixPathInTitle") as number as  boolean
	end try
	try
		tell application id "sevs"
			if not (UI elements enabled) then set (UI elements enabled) to true (* to be sure than GUI scripting will be active Yvan Koenig*)
			set prevApp to (name of every process whose frontmost is true and visible is true) as text
			tell application process "Finder"
				if visible of it is false then set visible of it to true
				set fwc to count its windows
			end tell
		end tell
		if fwc = 0 then return 0
	on error
		return 0 # Finder wasn't running!
	end try
	if fwc > 20 then
		ignoring application responses
			tell application id "com.apple.systemuiserver"
				activate
				try # 
					display dialog "Finding Duplicate Finder Windows in current space" with title my scriptTitle buttons {"Ok"} default button 1 with icon my FinderIcon giving up after ((fwc div 10) - 1)
				end try
			end tell
		end ignoring
	end if
	
	# COLLECTING THE FINDER WINDOWS
	
	
	script o # script for enhancing speed, not needed really 
		property ides : {} # ides of wins
		property targs : {} # targets
		property tmpTargs : {} # third pair of  tmp lists !
		property tmpIdes : {}
	end script
	
	
	local startTargs, startIdes # first pair of tmp lists for collecting
	
	tell application id "MACS" # collecting targets and ids of Finders windows, as fast as possible.
		set {startTargs, startIdes} to {name, id} of its every Finder window
	end tell
	
	# PREPROCESSING OF FINDER-DATA BEFORE WE CAN LOOK FOR DUPLICATES
	
	# Finding the last finder window in our space, we have grabbed THEM all. Possibly to many, if we have open
	# file info windows and such in this space. Finder windows in other spaces has a higher index, than our 
	# window count as obtained from System Events.
	
	local fwcAdj # Finder window count  Adjusted 
	set {o's ides, fwcAdj} to {items 1 thru fwc of startIdes, fwc}
	tell application id "MACS"
		repeat
			tell its Finder window id (item fwcAdj of o's ides) to if index of it > fwc then
				set fwcAdj to fwcAdj - 1
			else
				exit repeat
			end if
		end repeat
	end tell
	
	if fwcAdj = 0 then return 0
	
	set {o's targs, o's ides} to {items 1 thru fwcAdj of startTargs, items 1 thru fwcAdj of startIdes}
	
	if pathInTitles = true then
		# Sifting out any computer windows.
		# we have to convert the Targets into text, The spotlight windows, just disappears in the process
		# as we get a runtime error, ォclass ccmpサ is a way to say computer-object outside a finder tell block.
		local curId, siftedTargs, siftedIdes, hadCompWin, curWinCount # second pair of tmplists for sifting and preparing
		
		set {siftedTargs, siftedIdes, hadCompWin, curWinCount} to {{}, {}, false, fwcAdj}
		repeat with i from 1 to fwcAdj
			
			if (item i of o's targs) = "" then
				set curWinCount to curWinCount - 1
				try
					set curId to item i of o's ides # curtag used for minimizing references
					tell application id "MACS"
						if class of (target of (its Finder window id curId)) = computer-object then
							if hadCompWin = true then
								close Finder window id curId
							else
								set hadCompWin to true
							end if
						end if
					end tell
				end try
			else
				set end of siftedTargs to item i of o's targs
				set end of siftedIdes to item i of o's ides
			end if
		end repeat
		set fwcAdj to curWinCount
		if fwcAdj = 0 then return 0 # if there is nothing left, we'll leave the party!
		set {o's ides, o's targs} to {siftedIdes, siftedTargs}
		#		set {startTargs, startIdes} to {missing value, missing value}
	end if
	# no point in the above if we are not showing posix paths in the titles, as then text will show up!
	
	local searchTarg, searchIde, tids, searchlist, idx, idList, hadToSkip, skipList
	# used for having a searchlist, and the index for found elements
	# idlist is the list of ids of finder windows we are going to close 
	set {tids, idList} to {AppleScript's text item delimiters, {}}
	repeat
		# this construct will fail when there are only one item left on the list.
		try
			# start by removing blockers that we put on if we have to skip 
			
			set {searchTarg, searchIde, o's targs, o's ides} to {item 1 of o's targs, item 1 of o's ides, items 2 thru -1 of o's targs, items 2 thru -1 of o's ides}
			set searchlist to o's targs
			# an inlined handler indexofItem by Emmanuel Levy
			set AppleScript's text item delimiters to return
			set searchlist to return & searchlist & return
			set AppleScript's text item delimiters to ""
			try
				set idx to -1 + (count (paragraphs of (text 1 thru (offset of (return & searchTarg & return) in searchlist) of searchlist)))
				set {hadToSkip, skipList} to {false, {}}
			on error
				set idx to 0
			end try
			
			repeat while idx > 0
				local dontSkipit
				set dontSkipit to true
				# todo: n蚌 vi ikke har posix pathnames, s sjekker vi targets her, om det er samme target.
				# hvis det ikke er det, s er index fortsatt ulik 0, s vi fortsetter  sammenligne...
				if pathInTitles = true then
					set end of idList to item idx of o's ides
				else
					try
						tell application id "MACS" to if (target of (Finder window id searchIde)) = (target of (Finder window id (item idx of o's ides))) then
							set end of idList to item idx of o's ides
						else
							set {dontSkipit, hadToSkip, end of skipList} to {false, true, idx}
						end if
					on error
						# spotlight windows 
						set {dontSkipit, hadToSkip, end of skipList} to {false, true, idx}
					end try
				end if
				if dontSkipit then
					if idx = 1 then
						set {o's targs, o's ides} to {rest of o's targs, rest of o's ides}
					else if idx = length of o's targs then
						set {o's targs, o's ides} to {items 1 thru -2 of o's targs, items 1 thru -2 of o's ides}
					else
						set {o's targs, o's ides} to {items 1 thru (idx - 1) of o's targs & items (idx + 1) thru -1 of o's targs, items 1 thru (idx - 1) of o's ides & items (idx + 1) thru -1 of o's ides}
					end if
				else
					# we must skip it, as the name isn't denoting the same folder, we should still continue searching for others though
					set item idx of o's targs to "_@_" & item idx of o's targs
				end if
				set searchlist to o's targs
				set AppleScript's text item delimiters to return
				set searchlist to return & searchlist & return
				set AppleScript's text item delimiters to ""
				try
					set idx to -1 + (count (paragraphs of (text 1 thru (offset of (return & searchTarg & return) in searchlist) of searchlist)))
				on error
					set idx to 0
				end try
				
				if idx = 0 and hadToSkip = true then # have to remove our trick before we commence 
					repeat with skippedWindow in skipList
						local okTarg
						set AppleScript's text item delimiters to "_@_"
						set okTarg to text items of item skippedWindow of o's targs
						set AppleScript's text item delimiters to ""
						set item skippedWindow of o's targs to okTarg as text
					end repeat
				end if
			end repeat
			
		on error
			exit repeat
		end try
	end repeat
	
	set AppleScript's text item delimiters to tids
	
	set clcount to length of idList
	if clcount ≠ 0 then
		tell application id "MACS"
			repeat with wid in idList
				tell its Finder window id wid to close
			end repeat
		end tell
		if clcount = 1 then
			set msgText to "I closed 1 window!"
		else
			set msgText to "I closed " & clcount & " windows!"
		end if
	else
		set msgText to "Nothing to do!"
	end if
	tell application id "MACS"
		activate
		try # comment out the line below when testing for speed
			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
	tell application prevApp to activate
end script

tell pruneDupFWins to run


[ Reply to This | # ]