Tugulab Blog.

How to Get Confirmation Alert Before Quitting Safari on macOS

One infuriating thing about Safari on macOS is that from time to time I quit it unintentionally. It’s when I try to close a tab. I press…
author avatar

clacla

One infuriating thing about Safari on macOS is that, from time to time, I quit it unintentionally. It’s when I try to close a tab. I press ⌘Q instead of ⌘W and boom, 30 tabs are lost. Sure I can reopen them from the previous session, but still. Ffffff…

After a quick research I have found a good solution. It is a two part tutorial:

  1. Remap the current ⌘Q for Safari to ⌥⌘Q, so you will not trigger the Quit unintentionally
  2. Create a script to show a Quit confirmation alert when ⌘Q is pressed

Note: the second step is optional.

Remap ⌘Q in Safari

Currently Safari has ⌘Q as the Quit shortcut.

Open Preferences > Keyboard > Shortcuts. Select App Shortcuts, then tap the + button.

Fill the prompt as follows:

  • Select Safari for Application
  • Type Quit Safari in Menu Title (the title should be this exact one)
  • Press ⌥⌘Q on your keyboard after tapping in the Keyboard Shortcut field

Tap Add button when you’re done.

Now Safari has ⌥⌘Q as the Quit shortcut.

Add a confirmation alert for Quit Safari

Open the Script Editor app. Copy and paste the following code:

tell application "Safari"    set _window_count to count windows    set _tab_count to 0        repeat with _w in every window        set _tab_count to _tab_count + (count tabs of _w)    end repeat        -- Make a string like "1 window containing 3 tabs."    if _window_count is 1 then        set _msg to _window_count & " window containing " as string    else        set _msg to _window_count & " windows containing " as string    end if    if _tab_count is 1 then        set _msg to _msg & _tab_count & " tab." as string    else        set _msg to _msg & _tab_count & " tabs." as string    end if        display alert ¬        "Are you sure you want to quit Safari?" message _msg ¬        buttons {"Quit", "Cancel"} ¬        giving up after 60    if button returned of result is "Quit" then quitend tell

Save the script as SafeQuitSafari in some temporary location.

Download and install the free scripts management app FastScripts. Open it.

Make sure you have a Safari window open and in focus (important). Then tap on the FastScript top menu bar icon and select the Open Safari Scripts Folder from the FastScripts submenu.

Move SafeQuitSafari script inside the opened folder.

By clicking on the FastScripts icon in the top menu bar, you will see the new script listed (if Safari window is in focus).

Test it

Open Safari again and tap ⌘Q. You will now get the confirmation alert. Cheers 🍻