Some Apple Scripts for Safari
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.
Published on 🔗, tagged:
If you have any feedback, please comment below or contact me at hi@ybbond.id
You can also reply this tweet, that will be linked to this post: »Twitter Link«
Webmentions ?:
Submit your WebMention reply URL:
Does not know WebMention?
Use commentpara.de to reply: