I thought this setup I have might be useful to others. In Safari, I have Touch Bar buttons that essentially act the same as the app switcher, but for tabs — so, the currently open tabs are ordered by how recently I’ve viewed them, and I can tap on any of the buttons to switch to that tab. You can have as many of these as you want — more will mean that you can switch to tabs you were at farther back in the past.
Here’s an example (in between the app switcher and colored buttons):
In this example, I have 7 tabs open, so this shows the 6 that aren’t active. I’ve set these buttons to have a small font size (11), no space between them, and a corner radius of 0 (except on the left of the first one), although you might want to do something different.
Preset file:
Recent Safari Tabs v1-0-5.bttpreset (15.9 KB)
While the original issue of appearing/disappearing is no longer a problem, it now seems that sometimes when I switch away from Safari, I’ll be returned back immediately, and so occasionally have to make one or two tries to leave the app. I’m not sure if that will be an issue for others.
(Original post, with a more complicated version)
Whenever I’ve used these, I’ve had an issue where buttons repeatedly disappear and reappear, seemingly at random. It makes browsing distracting and also means that I can’t be sure which button I’m about to tap, so I’ve unfortunately had to disable these buttons most of the time. So, if anyone tries this setup, I’m curious whether you experience this issue, and if so, whether you are able to find the reason it’s happening (since I haven’t been able to).
To make this work, I have a text file which stores a list of the names of all the tabs I have open (including the active one), in order of how recently they were open. All of the buttons access this list to display the tab names and to switch to the tab when pressed, and the first button also serves the purpose of updating the list.
Here’s the preset:
Recent Safari Tabs.bttpreset (15.0 KB)
You’ll have to change every instance "[insert location of file]" to point to the actual file you’re using to store the tabs.
Each button is a “Run Apple Script and Show Return Value” widget. All of them have the following AppleScript to display the name of the tab (which runs multiple times a second so they will stay updated when I open, close, or switch tabs):
set fileRef to POSIX file "[insert location of file]"
set tabFile to (read fileRef as «class utf8»)
--All the tabs in the list, in order of how recently I visited them:
set tabs to paragraphs of tabFile
--The position in the tab list that this button represents
--(1 is the current tab, 2 is the most recent other tab, etc.):
set tabNum to 5 --change the number for each button; I started with 2
--The maximum number of characters that will be displayed on the button:
set dispLen to 10
--If there aren’t enough tabs for this button to be used, hide it:
if length of tabs < tabNum then
return ""
end if
--Get the name of this tab and cut it to the desired length:
set t to item tabNum of tabs
if length of t > dispLen then
set t to (characters 1 thru dispLen of t) as string
end if
--Remove spaces from the end:
repeat while character -1 of t = " "
set t to (characters 1 thru -2 of t) as string
end repeat
--Return the formatted tab name:
return t
The first button, which manages the list of tabs, has this in addition to the above:
--Every open tab:
tell application "Safari" to set openTabs to name of every tab in window 1
--The active tab:
tell application "Safari" to set currentTab to name of current tab of window 1
--If there have been any new tabs opened, add them to the list:
repeat with t in openTabs
if tabs does not contain t then
set tabs to {t as string} & tabs
end if
end repeat
--If there have been any tabs closed, delete them from the list:
set tabs_ to {}
repeat with t in tabs
if openTabs contains (t as string) then
set tabs_ to tabs_ & {t as string}
end if
end repeat
set tabs to tabs_
--Find the index of the active tab in the list:
set pos to 1
repeat with i from 1 to length of tabs
if item i of tabs = currentTab then
set pos to i
stop
end if
end repeat
--If it’s not at the top, move it to the top
if pos ≠ 1 then
if pos = length of tabs then
set tabs to {item pos of tabs} & items 1 thru (pos - 1) of tabs
else
set tabs to {item pos of tabs} & items 1 thru (pos - 1) of tabs & items (pos + 1) thru end of tabs
end if
end if
--Create the string to update the file, and update the file:
set tabString to ""
repeat with t in tabs
set tabString to tabString & (t as string) & "
"
end repeat
set tabString to (characters 1 thru -2 of tabString) as string
set eof of fileRef to 0
write tabString as «class utf8» to fileRef
Then, every button runs the following when tapped:
set fileRef to POSIX file "[insert location of file]"
set tabFile to (read fileRef as «class utf8»)
--All the tabs in the list, in order of how recently I visited them:
set tabs to paragraphs of tabFile
--The name of this tab (change the number for each button):
set t to (item 5 of tabs) as string
--Find the index of this tab in Safari, and switch to it:
tell application "Safari"
repeat with i from 1 to number of tabs in window 1
if name of tab i of window 1 = t then
set current tab of window 1 to tab t of window 1
end if
end repeat
end tell
Again, every time I use this, there’s the issue of buttons that keep flashing in and out of existence (even though the list itself does not change). So, it may or may not work for you.