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


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 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år 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 | # ]