Quit Confirmation for Safari on MacOS

Saw at Daring Fireball this script, which displays a prompt:

works great in KM


use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

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
    set _msg to _window_count & " windows containing " & ¬
        _tab_count & " tabs." as string

    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

source: https://daringfireball.net/2020/01/quit_confirmation_for_safari_on_macos

Nice idea to include the tabs and window counts like that. The only downside to this method is that it's a good bit slower than a macro with a much simpler variation of this idea that I've used for years now:

⌘Q Intercept.kmmacros (2.3 KB)

With one Safari window and 19 tabs, the AppleScript version takes around 0.6 seconds to appear, compared to 0.03 for the simple KM alert version. Nothing wrong with preferring the AS version if you don't mind the difference in speed, but for those who do, I offer this macro as an alternative.

Q: A cosmetic question: Is there a way to change or hide the KM icon?

@hello - I saw the same article and John referenced FastScripts. I was like, KM can do that as well. Here is pretty much the same thing.

Quit Safari Safely.kmmacros (3.4 KB)

thanks @kcwhat!

EDIT: ok, so I tried extending @gglick macro to work in more than one app, this works:


Yep. It's a safe technique.

I initially learned it from Mark's post several years ago.


1 Like

Not that I'm aware of. I agree that having the app icon in the AS prompt is a nicer touch than having the KM icon appear regardless of app, but I'll still take the speed advantage any day.

Incidentally, it occurs to me now that the macro I've been using is so old that it uses a deprecated token, %CurrentApplication%, rather the new, recommended one, %Application%1%. While %CurrentApplication% obviously still works, I think it's worth noting here for posterity in case it does ever stop working.

On a whim I just tested the script in FastScripts, and it lives up to its name; FastScripts does indeed run the script faster than KM does, even when using a complied script file, to the point where the delay in invoking it is significantly less noticeable. But it does still have a bit of delay, whereas the KM version is so fast as to feel instantaneous, so I'll continue to use the latter for my own needs.