Prune Duplicate Finder Windows in current space

Hello!

This script prunes duplicate Finder Windows at a reasonable pace. It should be relevant for at least some of you! :slight_smile:
It considers Spotlight windows, and doesn’t close them. It is more Nigel’s work, than mine, since he provided the eminent handler for finding the sibling of a list element.


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)
	-- © McUsr and put in Public Domain. Don't post this as it is elsewhere, or put it into a Public Accessible Repository or Library
	-- You may use it as you see fit otherwise, but please refer to this post for reference: http://macscripter.net/edit.php?id=155921
	local astid, p, q
	set astid to AppleScript's text item delimiters
	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 q to p mod n
		if n = 3 and q > 1 then set q to q - 1
		if n = 4 then
			if q = 2 then set q to q - 1
			if q > 2 then set q to q - (n - q) - 1
		end if
		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) + q
	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

Hmm. Good idea.

Thought I’d have a go.


-------------------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2013-05-06 : 19:39
# dMod: 2013-05-06 : 20:28
# Appl: Finder
# Task: Close Duplicate Windows
# Deps: general.lib
# Tags: @Applescript, @Finder, @Window, @Close, @Duplicates
# Note: Skips Spotlight search windows.
-------------------------------------------------------------------------------------------
try
	
	tell application "Finder"
		
		# activate --» Unnecessary if run from the Finder via script-runner.
		set AppleScript's text item delimiters to linefeed
		
		tell windows
			if (get collapsed) contains true then set collapsed to false
			set {winID, winTarg} to {id, paragraphs of (target as text)}
		end tell
		
		repeat with i from 1 to length of winTarg
			set _win to item 1 of winTarg
			set winTarg to rest of winTarg
			if (_win is in winTarg) and (_win ≠ "") then close window id (item i of winID)
		end repeat
		
	end tell
	
on error e number n
	set e to e & return & return & "Num: " & n
	tell me to set dDlg to display dialog e with title "ERROR!" buttons {"Cancel", "Copy", "OK"} default button "OK"
	if button returned of dDlg = "Copy" then set the clipboard to e
end try
-------------------------------------------------------------------------------------------

Hello.

My latest version of the script is here in post #42.

It’s fast and fairly robust. Hfs/Posix paths in window titles, Spotlight windows etc.

By the way.

Very good to see you around! :slight_smile: