I am wondering whether such a thing be possible to do quickly. With just a press of a hotkey, all the current tabs that are open except the one activated now and the pinned tabs would close. It would save me quite some time.
Right now the solution I can see is getting current url of my current tab, close the safari window, open it again and paste it in but that is really undesirable as first it takes time to close and then open the safari window and secondly I just feel there would be a more elegant solution than this.
Here’s a start. It should close all tabs except the current one (but see notes below):
activate application "Safari"
tell application "Safari"
set currentWindow to window 0
set tabCount to count of tabs of currentWindow
repeat with i from tabCount - 1 to 0 by -1
if index of tab i of currentWindow is not equal to index of current tab of currentWindow then
close tab i of currentWindow
end if
end repeat
end tell
If you have more than one window open, you might need to change the “set currentWindow” - probably not but I didn’t test it.
Just ran your script and it really is a great start, however it does close all my pinned tabs too. I hope there is a way to keep them open. You guys are really amazing with how fast you can think of solutions like this.
In order to determine if a tab is pinned or not, I had to resort to checking to see if the "Unpin Tab" menu item exists for that tab. So that means I had to switch to each tab, and wait until the tab is actually loaded before checking the menu item. Which slows down the macro drastically.
So if you look in the script, you'll see "delay 0.5". It means "delay a half second". You may need to adjust this higher ("delay 0.7", "delay 1.0", etc) to get it to work consistently. So if you notice pinned tabs being closed, adjust this value.
There may be other ways of doing this. Maybe someone else will jump in with a better way that doesn't require delays.
Thank you a lot Dan, this is indeed a solution however it does take unbearably long time especially given that my use case with this macro will be when I have 10+ tabs open. I will be trying to use this as an alternative for now so thank you a lot for taking the time and writing it out. I really appreciate it.
The first solution is so fast and smooth that it gives me hope there is a chance that something like this can be done with the pinned tabs staying intact.
I think this will work, although it's possible I have a bug or two in it, time will tell.
I made a lot of assumptions, not all of them thrill me, but it's a start. Assumptions:
Either you only have one Safari window open, or the API method "windows[0]" always returns the front window.
You don't have more than one tab open with the same name. If this is not true, it will either keep both of them open, or close both of them, depending on the situation.
A bunch of other assumptions that are probably correct, but you know what they say about assumptions...
Let me know. If it works, it's plenty fast, as in almost instantaneous.
This works just as I wanted it too. All of your assumptions are correct and this works exactly like the native 'close other tabs' does and just as fast
The only thing is that I get this message whenever there are two tabs of same name open :
Instead of it closing both of them or leaving them open. Thank you so much Dan.
Also remember this post that you helped me with? I want make it so that this modified clipboard from my 2Do app will get piped to Bitbar or something alike so that I can then display the task in my menu bar. I was just wondering, would such a thing be possible to do? I remember seeing Bitbar menu bar icon from one of your screenshots so I presume you are familiar with it.
This would indeed be really nice to make as I will be able to see a current task I want to work on in my menu bar and keep focused on the work. Essentially I just want to display text that will get piped into it from the content of the clipboard. What do you think of it? Would it be hard to do you think?
The basics of the normal AppleScript method for dealing with this (Dan did it in JXA).
I’m not bothering to retain the current tab, since there’s a keyboard shortcut for that.
-Chris
--------------------------------------------------------------------------------
tell application "System Events"
tell application process "Safari"
tell front window
tell tab group 1
set pinnedTabList to name of radio buttons whose role description is "pinned tab"
end tell
end tell
end tell
end tell
tell application "Safari"
tell front window
(name of tabs)
repeat with theTab in (get name of tabs)
if theTab is not in pinnedTabList then
close tab theTab
end if
end repeat
end tell
end tell
--------------------------------------------------------------------------------
Just came across this thread. I’m not sure if this is a new hotkey but if you press OPTION-COMMAND-W it closes all tabs but the current. Easy to script in KM.