ybbond

My site. The main domain
Log | Files | Refs | README | LICENSE | CC-LICENSE

commit 415fc8830956635a955c9ea23c41a514257b2dc9
parent bb3815d690222f1f62cf8f77473e9e4caa69860b
Author: Yohanes Bandung Bondowoso <hi@ybbond.dev>
Date:   Mon, 24 Aug 2020 01:16:34 +0700

content: add content about Apple Scripts for Safari

Diffstat:
Acontent/posts/Some Apple Scripts for Safari.md | 112+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

1 file changed, 112 insertions(+), 0 deletions(-)

diff --git a/content/posts/Some Apple Scripts for Safari.md b/content/posts/Some Apple Scripts for Safari.md
@@ -0,0 +1,111 @@
+---
+title: Some Apple Scripts for Safari
+author: Yohanes Bandung Bondowoso
+description: macOS provides way to change default key bindings for any applications, and changed some of Safari's key bindings with it too. I discovered other way to "change" or add key bindings with Apple Script and FastScripts. It is awesome that I'd like to share it.
+tags:
+  - tools #tools
+  - apple #apple
+twitter:
+  link: https://twitter.com/ybbond_/status/1297602893504106496
+slug: safari-apple-script
+date: 2020-08-23T19:48:48+0700
+ID: 20200823194848
+---
+
+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](https://github.com/televator-apps/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](https://daringfireball.net/2020/01/quit_confirmation_for_safari_on_macos) via DaringFireball.net.
+
+For your convenience, I will share the slightly modified script here:
+
+```AppleScript
+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](https://red-sweater.com/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](https://help.red-sweater.com/fastscripts/faq/).
+
+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:
+
+```AppleScript
+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:
+
+```AppleScript
+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](https://nestedfolderspodcast.com) and [automating some tasks](https://www.relay.fm/automators), and others. Now I know how awesome the previously overlooked tools is.+
\ No newline at end of file