Safari Technology Preview is my primary web browser when I am not developing website. It is the beta equivalent for Safari that features the latest updates, including the Β«Tracker BlockerΒ» that will be released to public on macOS Big Sur.
One of the main losses is the lack of good browser extension to simulate Vim key bindings. Vimari is okay, but it doesnβt work correctly on GitHub. The other losses are the lack of accidental quit prevention and alternative key binding to change tab.
Prevent accidental quit
Firefox and Chrome has option to warn us if we press β + Q
accidentally. Safari doesnβt provide this option. This often happen when I want to close a tab with β + W
, but my clumsy ring finger misses that W
. Previously, I use App Shortcuts
modifier (System Preferences β Keyboard β Shortcuts β App Shortcuts) and assign it to β + β₯ + Q
. It works, but I am curious whether thereβs another way.
I browsed the internet, and found that John Gruber already shared the solution for this issue. You can read it on this post: Quit Confirmation for Safari on MacOS via DaringFireball.net.
For your convenience, I will share the slightly modified script here:
tell application "Safari Technology Preview"
set windowCount to count windows
set tabCount to 0
repeat with currWindow in every window
set tabCount to tabCount + (count tabs of currWindow)
end repeat
-- Make a string like "1 window containing 3 tabs."
if windowCount is 1 then
set msg to windowCount & " window containing " as string
else
set msg to windowCount & " windows containing " as string
end if
if tabCount is 1 then
set msg to msg & tabCount & " tab." as string
else
set msg to msg & tabCount & " tabs." as string
end if
display alert Β¬
"Are you sure you want to quit Safari?" message msg Β¬
buttons {"Cancel", "Quit"} Β¬
giving up after 60
if button returned of result is "Quit" then quit
end tell
Notice that you might want to change from Safari Technology Preview
to only Safari
.
Youβd want to put the script to Script Editor.app
, and βas suggested by John, use FastScripts. FastScripts is awesome! Credits to red sweater. What you should do is move the scripts generated by Script Editor to FastScriptsβ folder for Safari and assign your preferred key bindings for it. Obviously youβd want to assign it to β + Q
.
More info on FastScripts, you can check their FAQ here.
Because weβve covered the issue about quitting, now moving to the next issue.
Alternative key bindings to change tab
The default key bindings to change tab for Safari is β + β₯
for Next Tab, and β + β§ + β₯
for Previous Tab. Iβd like to add the already familiar key bindings that exist on Firefox and Chrome, that is β + β₯ + β
for Previous Tab and β + β₯ + β
for Next Tab.
I tried using Macβs built in App Shortcuts
modifier (System Preferences β Keyboard β Shortcuts β App Shortcuts) to modify it, but Iβd lose the original key bindings. I want them both to coexist.
My solution is by using the Apple Script and FastScripts too.
Hereby the script to change tab to Next Tab:
tell application "Safari Technology Preview"
tell window 1
set myTab to (index of current tab)
set goodtab to (myTab + 1) as integer
try
set current tab to tab goodtab
on error
set current tab to tab 1
end try
end tell
end tell
and the script to change tab to Previous Tab:
tell application "Safari Technology Preview"
tell window 1
set myTab to (index of current tab)
set goodtab to (myTab - 1) as integer
if goodtab = 0 then
tell application "Safari Technology Preview" to set lastTab to the number of tabs in window 1
set current tab to tab lastTab
else
set current tab to tab goodtab
end if
end tell
end tell
Youβd want to change the Safari Technology Preview
too. The scripts are straight forward, one of the advantages of Apple Scriptβs strange syntax. Let me explain a bit about the try ... on error
and if ... else
part.
When you use the Next Tab script and reach the last tab, youβd encounter error because the last tab + 1 is nowhere to be found. In that case, we move to the first tab. Thatβs the expected behavior too, because weβd want the key binding to keep looping on the available tabs.
For the Previous Tab script, it gets a little complicated, because there is no property or method to get the length of available tabs that I know of. So I ask (tell
) Safari to define a lastTab
variable with the number of available tabs on the active Safari window as its value.
Prior to this day, I never thought that Iβd use Apple Scripts and these kind of macOS utility tools helps my day this much. I read Mac Power User, listen to Rosemary Orchardβs podcasts about productivity and automating some tasks, and others. Now I know how awesome the previously overlooked tools is.
Top comments (0)