Safari Tab Management on the Mac

I’ve been a Google Chrome user for many years, but with my regular use of i*OS and spending more time using my Macbook Pro out and about, battery life management and sharing of bookmarked sites across device has become more important for my everyday use. As a result, I’ve been using Safari as my default Mac web browser.

But there are a few tab management features that I was missing from Chrome, so a while back I set about adding those in using some of the tools I had at my disposal.

Tab Closure

The first thing that I found that I missed was being able to close tabs to the right of the current tab. I often go off researching something, then find the solution and have a dozen extra tabs open. Being able to close tabs to the right of my starting tab is therefore a useful option.

Fortunately Safari provides access to basic tab management functionality via its AppleScript library. This meant that with just a little bit of AppleScript I can send a command to Safari to close the tabs to the right of the currently selected tab in the current Safari window.


tell window 1 of application "Safari"
	close (tabs where tabIndex > (get tabIndex of current tab))
end tell

Sometimes I also open a new window and just want to close everything that came before a tab where I found what I was looking for. For this it is just a case of flipping the inequality on the second line like so.

tell window 1 of application "Safari"
	close (tabs where tabIndex > (get tabIndex of current tab))
end tell

Note that there is a built-in option to close all tabs other than the current one. This can be accessed from the tab context menu, or via the file menu with option held down (keyboard shortcut: w). No AppleScript necessary for the double direction closure.

Triggering the Keyboard Maestro Macros

Whereas I used to keep all of my AppleScripts accessible from the Scripting menu, I now prefer to keep them accessible via Keyboard Maestro. Keyboard Maestro is my central repository for app automations in many ways, and while I could spread them across many apps and locations, I find that Keyboard Maestro gives me the greatest level of control and flexibility for accessing them.

With these set-up in Keyboard Maestro, I could use keyboard shortcuts to trigger them, but what I actually have is two alternatives. I have the Keyboard Maestro macros triggerable via both Better Touch Tool and via an Elgato Stream Deck.

When I’m set up for working for an extended period, I have a physical piece of hardware plugged into my Macbook Pro. This is a bank of physical keys each with its own screen that can be set to do something. They can also be set to vary by the active application. In this case, when I’m in Safari, the layout offers me a set of keys/functions for working with Safari. They each trigger a Keyboard Maestro URL to run the macros. You can read more of my Stream Deck posts and a specific one about triggering Keyboard Maestro macros.

But I don’t always have my Stream Deck with me or the urge to unpack it, plug it in, tidy it away at the end, etc. For that reason I have also customised my Touch Bar using Better Touch Tool and added the same macro triggers there. It doesn’t feel as nice/tactile to use as the Stream Deck, but it is perfectly functional and probably quicker to access as my hands don’t need to really travel away from the keyboard … but I do like a tactile feel to my keys.

I’ve also set-up keys (Touch Bar and Stream Deck) for the double direction closure just so that I know I have a complete set to choose from. But in those cases, I can use the inbuilt tools just to send the key press. There’s no need to tie that into a Keyboard Maestro macro as it is built into Safari already.

Tab Duplication

The second tab management feature I miss from Chrome is tab duplication. Unfortunately I wasn’t able to fully reproduce this feature, but I have a close enough version for now.

With tab duplication in Chrome it creates and loads a new exact duplicate of the specified tab. Now by exact duplicate I mean that it not only has the current location, but the navigation history of all previous tabs. This history duplication is the aspect missing to this Safari solution. It will only duplicate the current tab in terms of taking the current tab location and opening that location a second time in a new tab.

This can be accomplished with a couple of keyboard shortcuts in succession:

  1. l

With the close all but the current tab keyboard shortcut, I elected to put that directly into the two triggering applications directly as both Better Touch Tool and Stream Deck support key press sending. But for this particular one, rather than put in the two successive keyboard shortcuts into both (which they both support), I chose to use Keyboard Maestro and to trigger them with URLs as per the other tab closing macros.

The reason for this is that at some point I hope to devote more time to this duplication and to figure out a way to also duplicate the history. That isn’t going to be a couple of keyboard shortcuts to produce, and so I expect it’ll end up needing to be in Keyboard Maestro. By putting the existing keyboard shortcuts into Keyboard Maestro, if/when I finally do update it, I won’t have to re-create the triggers. They’ll both be primed to work.

in comparison, the close other tabs solution is already feature complete and so doesn’t need to change and should trigger fractionally faster by not having to go via Keyboard Maestro.

Wrap Up

I think the move to Safari is absolutely the right one in my circumstances, even though I’m continuing to use Chrome as my primary browser for my work needs over on Windows (though the new Chrome based Edge browser is of interest for me working in a company based on Microsoft platforms). These little additions have certainly helped smooth the transition for me on the Mac, and I hope they’ll be able to do the same for some of you.

Author: Stephen Millard
Tags: | mac | applescript | keyboard maestro |

Buy me a coffeeBuy me a coffee

Related posts that you may also like to read