arrow-left arrow-right brightness-2 chevron-left chevron-right facebook-box facebook loader magnify menu-down rss-box star twitter-box twitter white-balance-sunny window-close
Select certain tabs in Safari (or WebKit) using keyboard shortcuts
3 min read

Select certain tabs in Safari (or WebKit) using keyboard shortcuts

One of my absolute favorite features of Firefox is the ability to quickly bounce back and forth between certain tabs by hitting cmd-<#> (e.g., cmd-1 takes you to the first tab, cmd-9 takes you to the last tab, etc.). I always keep Gmail in the first tab and Google Reader in the second, and I find myself jumping to the last tab as often as the first two.¹ Needless to say, I hit cmd-1, cmd-2 and cmd-9 constantly; it’s been pure muscle-memory for years.

The problem with Safari/WebKit

If you’ve ever tried cmd-<#> in Safari/WebKit, you know that it doesn’t work the same as in Firefox; instead of going to a specific tab, it loads, in the current tab, the bookmark from your Bookmarks Bar that corresponds to the particular number. For example, if the first bookmark in your Bookmarks Bar is for Gmail, then cmd-1 will load Gmail in your current tab. In my opinion, this system is an utter waste of some of the best available keyboard shortcuts (and it really screws up my browser workflow).

The solution

Fortunately, as of Safari/WebKit v3.0, you actually can target particular tabs with AppleScript. With that in mind, the solution involves invoking a particular AppleScript for the particular tab position you want to jump to, and assigning the invocation to a certain keyboard shortcut (e.g., cmd-1).²

With respect to the second half of the solution — i.e., correlating certain keyboard shortcuts with the execution of certain AppleScripts — you’re going to want to get something like Daniel Jalkut’s (fantastic!) FastScripts.³

As explained above, I care about being able to bounce between just three tabs — (1) Gmail, (2) Google Reader and (x) the last tab — and so I created three AppleScripts, as shown below. (Though I list the three AppleScripts I created for my use case, let’s just assume for the rest of this piece that you only want to be able to jump to the first tab.)

Go to the first tab:

tell front window of application WebKit to set current tab to tab 1

Go to the second tab:

tell front window of application WebKit to set current tab to tab 2

Go to the last tab (I assign this to cmd-3, instead of Firefox’s cmd-9):

tell front window of application WebKit to set current tab to last tab

After creating the AppleScript, name it whatever you like, and save it to ~/Library/Scripts/Applications/WebKit/. If you want this to work in Safari, then you’ll need to change each instance of WebKit to Safari. If you want this to work in either browser, but with just a single AppleScript for each tab you want to target (instead of two), check out Gruber’s solution for dynamically targetting either Safari or WebKit.

After saving the AppleScript, go to System Preferences > Keyboard & Mouse > Keyboard Shortcuts. Click the + sign, choose WebKit and give Menu Title the same name as your first Bookmarks Bar bookmark (i.e., what currently is associated with cmd-1). For Keyboard Shortcut, just make up some nonsensical shortcut you’ll never actually want to use (e.g., cmd-option-ctrl-x).

Next, jump to Preferences > Script Shortcuts within FastScripts; the application should have noticed when you earlier saved the AppleScript to the WebKit folder, and so WebKit should be in the list of applications presented to you. Click the triangle next to WebKit and you should see the AppleScript you saved. All that’s left to do is to assign the cmd-1 keyboard shortcut to the AppleScript, and maybe restart WebKit.&sup4;


FOOTNOTES
  1. It often is the case, as I’m blazing through Google Reader or reading an article in another tab, that I’ll want to switch to the link I just opened in a background tab, and instead of having to scroll to it, I just punch cmd-9. I usually have 75-100 tabs open, so scrolling them to the end (and eventually back to the first and second tabs) can be very annoying in either browser (even if you use something like SafariStand, which allows you to actually scroll through all of them in Safari/WebKit).

  2. Yes, I could have done this through Quicksilver (or the fantastic Launchbar, which is what I currently use (much more on that in a future post)), and without using any AppleScript, but that requires a few more steps than I’d like for something I use so frequently.

  3. For those wondering, I’m now using FastScripts instead of SizzlingKeys with regard to giving my Griffin Powermate system-wide control of iTunes. (There was nothing wrong with SizzlingKeys, I just wanted to consolidate.)

  4. I’m really not sure whether restarting the application is necessary. For what it’s worth, there seem to be a lot of inconsistencies, at least on my machine, with regard to when the keyboard shortcuts actually take — it’s quite frustrating. To that end, it’s still not entirely clear to me that you have to assign some other keyboard shortcut to bookmarks in the Bookmarks Bar before using FastScripts to associate cmd-<#> with the respective AppleScript. Indeed, as I type this, my cmd-1, cmd-2 and cmd-3 FastScripts assignments are working beautifully within WebKit, but the WebKit menus still show cmd-2 and cmd-3 next to the second and third bookmarks in my Bookmarks Bar, while the correct, nonsensical shortcut appears next to the first bookmark. shrug

You've successfully subscribed to Justin Blanton.
Success! Your account is fully activated, you now have access to all content.