Open farthest right tab in Safari

safaritabs

In Chrome, one can press cmd+1 to open the first tab, cmd+3 to open the third tab, etc… Safari has replicated this behavior now but has not replicated cmd+9 which takes the user to the far right tab even if there are dozens of tabs in the window. Is there a way to map cmd+9 or another key mapping to that action?

Best Answer

That was a fun question to tackle! Follow the instructions below and paste this code in step 1.8:

set n to 0

tell application "Safari"
    repeat with w in windows
        try
            set n to n + (count tabs of w)
        end try
    end repeat
end tell



tell front window of application "Safari"
    set current tab to tab n
end tell

This will tell Safari to change to the right most tab. Then, to map it to a key shortcut, just follow the steps listed here by Monomeeth which is shown below. The code was adapted from here

In summary you will need to use:

  • Automator to create a service that runs an Apple Script
  • System Preferences to assign a keyboard shortcut to the service

Below are the steps involved for each.

1. Create a service using Automator

  1. Launch Automator (usually found within your Applications folder)
  2. Go to File > New
  3. Select Service and click Choose
  4. In the top right hand of the window that appears, ensure that "No input" is selected from the Service receives drop-down list
  5. Ensure the In drop-down list is showing "Any application"
  6. In the second column containing the long list, find "Run AppleScript" and double-click on it
  7. This will add the Run AppleScript window on the right
  8. Now, replace the (* Your script goes here *) with the script you want (the one I provided above)
  9. Save the service using a meaningful name (e.g. RightMostTab).

Now to the next step.

2. Creating your shortcut

  1. Go to System Preferences > Keyboard > Shortcuts
  2. Select Services from the sidebar
  3. Find your service (it'll be in the list on the right-hand side)
  4. Add a shortcut by double clicking on the service name
  5. Now go to System Preferences > Security & Privacy > Privacy
  6. Select Accessibility in the sidebar
  7. Click on the + sign (you may need to unlock the padlock if it's locked)
  8. Add Automator.
  9. Add Finder (to find this you will need to navigate to /System/Library/CoreServices/Finder.app).
  10. Exit your System Preferences.

Let me know how it goes!