Reputation: 156
I'm looking for a way to close specific tabs in safari by the url. I would probably expand on something like this:
tell application "Safari"
repeat with t in tabs of windows
tell t
if name starts with "google.com" then close
end tell
end repeat
end tell
the problem here is that i haven't found a way how to get the url value of a tab and close them based on that.
Upvotes: 2
Views: 4401
Reputation: 867
It's kind of an impressive way of reducing memory usage. old answers don't work for me. this is my answer.
set closeURLs to {"google", "youtube"}
tell application "Safari"
set windowNumber to 1
repeat the number of windows times
repeat with theURL in closeURLs
close (every tab in window windowNumber whose URL contains theURL)
end repeat
set windowNumber to windowNumber + 1
end repeat
end tell
references
version (log purpose)
Safari: Version 17.4.1 (18618.1.15.111.8, 18618)
OS: Ventura 13.6.6 (22G630)
Upvotes: 1
Reputation: 750
You can get the URL from Safari based on the active tab.
tell application "Safari"
set w to first window
set t to current tab of w
display dialog (URL of t as string)
end tell
And then you could iterate over every tab/page like this :
tell application "Safari"
set windowCount to number of windows
repeat with x from 1 to windowCount
set tabCount to number of tabs in window x
repeat with y from 1 to tabCount
set thistab to tab y of window x
set foo to URL of thistab
if foo is not equal to "bar" then close thistab
end repeat
end repeat
end tell
Upvotes: 3
Reputation: 11238
Here is another method:
set closeURLs to {"http://www.yahoo.com", "http://www.apple.com"}
repeat with theURL in closeURLs
tell application "Safari" to close (every tab of every window whose URL contains (contents of theURL))
end repeat
Upvotes: 2