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: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 | # ]