Switching Safari tabs from BBEdit

I’ve written before about the automation I use when writing a blog post to quickly add a link to the frontmost Safari page. It’s kind of involved, including both Python and AppleScripting of BBEdit to add a Markdown reference link to the bottom of the current document and put the cursor back where it belongs. Its great value is that it lets me add a link by just pressing ⌃L—no switching between BBEdit and Safari or messing around with the clipboard. I can stay focused on what I’m writing. But there’s another automation I use in conjunction with it that I’ve never written about because it’s so ridiculously simple. And yet it’s just as important in keeping me on track.

Most of the time, I have BBEdit in the foreground and Safari behind it, with some number of Safari tabs open to the pages I expect to link to in the post.

BBEdit and Safari

If the page I want to link to is in the current tab, my linking automation works wonderfully. But if I want to link to one of the other tabs, I have to bring Safari forward, switch tabs, go back to BBEdit, and add the link. The app switching is an unnecessary interruption. To avoid that, I use this one-step Keyboard Maestro macro:

KM Next Safari Tab

There’s a similar one that uses the Previous Safari Tab action and is bound to ⇧⌘[.

With these two macros, I can navigate between Safari tabs just as I would if Safari were the active application. The Safari window in the background updates with every tab switch, so I can see what page it’s on. When I get to the right page, I use ⌃L to add a link to it.

Before I bought Keyboard Maestro, I had AppleScripts that worked similarly but not as smoothly. They were kept in BBEdit’s Scripts folder and were also bound to ⇧⌘] and ⇧⌘[. Here’s one of them:

applescript:
1:  tell application "System Events"
2:    tell process "Safari"
3:      set frontmost to true
4:      click menu item "Show Next Tab" of menu "Window" of menu bar 1
5:    end tell
6:    tell process "BBEdit" to set frontmost to true
7:  end tell

These worked, but there was a brief flash as Safari came to the front, did its tab switch, and then retreated as BBEdit was made frontmost again. It wasn’t particularly distracting, but once I learned that Keyboard Maestro could do the tab switch in the background, I stopped using the AppleScripts.

(Strictly speaking, only one of these macros is necessary, as the tabs act as a loop; when you get to the rightmost tab, the “next” tab is the leftmost one. But it’s more convenient to have both “next” and “previous” available, just as it is when you’re working in Safari itself.)

Now that we’ve entered the brave new world of Shortcuts on the Mac, you might be wondering if this can be done without Keyboard Maestro or AppleScript. As far as I can tell, the answer is no. If you want to switch tabs in a shortcut, it looks like you’ll have to call out to AppleScript.

Safari actions for Shortcuts on the Mac

If you’re a Chrome user, Keyboard Maestro has similar actions for switching tabs there. Nothing for Firefox, unfortunately, but you can use AppleScript and UI scripting:

applescript:
1:  tell application "System Events"
2:    tell process "Firefox"
3:      set frontmost to true
4:      keystroke "]" using {command down, shift down}
5:    end tell
6:    tell process "BBEdit" to set frontmost to true
7:  end tell

As with my old AppleScripts for Safari, this will cause a brief flash. The same idea could be used on any browser that has a keyboard shortcut for switching tabs.

I often forget to blog about these simple automations. I need to remember that simple and effective are not antonyms.

Update 3/5/2022 12:05PM
Shortly after this was posted, George Snow sent me an email with this clever “next tab” AppleScript that works entirely in the background and avoids the flashing problem of my scripts:

applescript:
 1:  tell application "Safari"
 2:    tell front window
 3:      set tabCount to count of every tab
 4:      set currentTab to (index of current tab)
 5:      set nextTab to (currentTab + 1)
 6:      if nextTab > tabCount then
 7:        set current tab to tab 1
 8:      else
 9:        set current tab to tab nextTab
10:      end if
11:    end tell
12:  end tell

Because it doesn’t use menu selections or keystrokes, Safari doesn’t have to be in the foreground, even for a split second. It takes advantage of the fact that Safari assigns tab numbers from left to right, starting at one. You can see how Lines 6–10 handle the situation when you’re already on the rightmost tab.

I’m pretty sure George’s script will work on Chrome with just a minor tweak or two. I don’t have Chrome installed on any of my Macs anymore, but I’m pretty sure it uses the same left-to-right tab numbering scheme.

Then kotfu sent me a tweet with a Keyboard Maestro-based solution for Firefox:

Use the Keyboard Maestro “Type A Keystroke” action, click the gear, choose “Send To”, and select “Firefox” from the list of apps.

Here’s a screenshot of kotfu’s solution:

Keyboard Maestro Send To submenu

That gear menu has lots of useful stuff. I really should look at it more often.