------------------------------------------------------------------------------------------------
# Author: Christopher Stone
# Created: 2012-04-25 : 13:30
# Modified: 2012-04-25 : 14:26
# Application: Safari
# Purpose: Close selected window(s)
# Dependencies: none
------------------------------------------------------------------------------------------------
try
tell application "Safari"
activate
set {docCount, docNames, tabCount, winIndex} to ¬
{count, name, count of tabs, index} of (every window where its document is not missing value)
if docCount < 10 then set docCount to "0" & docCount
set currentTabName to (name of current tab of front window)
set hdrText to {¬
"-----------------------------------", ¬
"Window Count: " & docCount, ¬
" Tab Count: " & tabCount, ¬
"-----------------------------------", ¬
"Select a window to close with ALL its tabs:", ¬
"-----------------------------------"}
set AppleScript's text item delimiters to return
set hdrText to hdrText as string
repeat with i from 1 to length of winIndex
set theIndex to item i of winIndex
set tabCnt to count of tabs of window theIndex
set _temp to " { Win " & theIndex & " » " & tabCnt & " Tabs }"
set _winRef to (a reference to item i of docNames)
tell _winRef
set its contents to (its contents) & _temp
end tell
end repeat
set selectedWindows to choose from list docNames ¬
with title "•• Safari Windows ••" with prompt hdrText ¬
default items {currentTabName} ¬
with empty selection allowed and multiple selections allowed
if selectedWindows = false then
return
else
set {oldTIDS, AppleScript's text item delimiters} to {AppleScript's text item delimiters, linefeed}
set selectedWindows to quoted form of (selectedWindows as text)
set AppleScript's text item delimiters to return
set winIndexesToClose to paragraphs of (do shell script "sed -E 's/^.+{ Win ([0-9]+).+/\\1/' <<< " & selectedWindows)
# Prevent shifting window indecies.
repeat with i in winIndexesToClose
set contents of i to window (i as integer)
end repeat
repeat with i in winIndexesToClose
close i
end repeat
set AppleScript's text item delimiters to oldTIDS
end if
end tell
on error eMsg number eNum
set {c, s} to {return, "------------------------------------------"}
set e to s & c & "Error: " & eMsg & c & s & c & "Error Number: " & eNum & c & s
beep
display dialog e
end try
------------------------------------------------------------------------------------------------