I searched everywhere including here for a way to close a tab. I have this code that should work:
tell application "Safari"
repeat with aWindow in windows
repeat with aTab in tabs of aWindow
if name of aTab is "Facebook" then
tell aTab to close
end if
end repeat
end repeat
end tell
I have a Facebook tab open and it doesn’t work, the code seemed to work when closing whole windows though.
See if you can adjust this, this is kind of a slow way to find a tab and close it, but it is the best I can do for now.
tell application "Safari"
set wins to every window whose visible is true
repeat with awin in wins
tell contents of awin
set n to count tabs
repeat with i from n to 1 by -1
if name of tab i is "Facebook" then close tab i
end repeat
end tell
end repeat
end tell
This doesn’t work for me in Safari 6.0.5 on OSX 10.8.4. It errors out.
But this works:
set tabName to "Google"
tell application "Safari"
tell (windows where its document is not missing value)
set _tabs to tabs where its name is tabName
repeat with ndx1 in _tabs
repeat with ndx2 in ndx1
close ndx2
end repeat
end repeat
end tell
end tell
Yes, why shoudn’t it? But I should really have done it like Chris Stone. That beeing said, I’m on Snow Leopard.
By the way here is a variation, that you can save as an applet, so that it checks your tab for duplicates now and then. (I’d never close a google page automatically.
It does close, all the tabs of a certain name, but the first, if that tab is current. And you can set the LSUIElement key in the plist file to true, and add it to your login items, so it works totally in the background, -on very sparse resources.
-- http://macscripter.net/viewtopic.php?pid=164214#p164214 post #6
property tabNames : {"Home", "NRK", "Hacker News", "BBC"}
# every word in this list has to be unique for good results
script removeDupTabs
script o
property l : {}
end script
local foundit
set foundit to false
local tabNamesWords
tell application "Safari"
set o's l to every window where its document is not missing value
repeat with t from 1 to (count o's l)
tell item t of o's l
set n to count tabs
repeat with i from n to 1 by -1
set b to name of tab i of item t of o's l
set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, " "}
set sitename to first word of (name of tab i of item t of o's l as text) as list
set AppleScript's text item delimiters to tids
if sitename is in tabNames then
if index of current tab of item t of o's l is i then
if foundit then
close tab i
else
set foundit to true
end if
else
close tab i
end if
end if
end repeat
end tell
end repeat
end tell
end script
on run
tell removeDupTabs to run
end run
on idle
tell removeDupTabs to run
return 900
end idle
This is a slight variation, to find out which tab are playing music, or showing a movie after reopening a session or something; it brings the windows with the search-text (youtube) to front, so you can pick and choose.
property stext : "youtube"
on run
script o
property l : {}
end script
local foundit
set foundit to false
tell application "Safari"
set o's l to every window where its document is not missing value
repeat with t from 1 to (count o's l)
tell item t of o's l
set n to count tabs
repeat with i from n to 1 by -1
if URL of tab i of item t of o's l contains stext or name of tab i of item t of o's l contains stext then
tell tab i of item t of o's l to do JavaScript "self.focus()"
end if
end repeat
end tell
end repeat
end tell
end run
There was a logical flaw in the stay-open in the background applet in post #6, for closing a certain tab there may be duplicates of, but it is fixed now.
In your original script you said tab name is ‘Facebook’. If I load up FB I get “(6) Facebook”
So lets try changing ‘is’ to ‘contains’:
set tabName to "Facebook"
tell application "Safari"
tell (windows where its document is not missing value)
set _tabs to tabs where its name contains tabName
repeat with ndx1 in _tabs
repeat with ndx2 in ndx1
close ndx2
end repeat
end repeat
end tell
end tell
I’ve been trying to puzzle out what the problem is, but most of yours and Chris’s suggestions I’ve tried work perfectly here. With regard to the methods involving repeats, your count-down approach is the way to go, since the tabs are returned to the script and used by it as index references, so deleting any of them throws out the higher indices.
Otherwise I’m flummoxed. Maybe it would help if Richard told us how many and what windows he has open, how many of them contain how many tabs named “Facebook”, what version of Safari he’s using, what else is going on in the script, etc.
I see you have a couple of empty tabs there ( {} {} ), now, I wonder if that can have something to do with either windows, that aren’t visible, or windows whose document is missing value.
The next step is to just make a list of the tabs of those windows, and see if we can get a list of tabs that have names, because I expect the script to err out when it encounters tabs without names.
Could you please run this, and see if the ( {} {} ) goes away? (Hopefully it works for you, still on Snow Leopard.)
tell application "Safari"
tell (every window whose visible is true and its document is not missing value)
set thetabs to name of tabs
end tell
end tell
It is ok, if they don’t go away, it is just unanticipated that they are there, after you have run the script above, but if they still are there, then we can circumvent the problem by
tell application "Safari"
set wins to every window whose visible is true and its document is not missing value
repeat with awin in wins
tell contents of awin
set n to count tabs
repeat with i from n to 1 by -1
if name of tab i is not missing value and name of tab i is "Facebook" then close tab i
end repeat
end tell
end repeat
end tell