Quit Confirmation for Safari on MacOS

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

Works great in KM

Script:

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: Daring Fireball: 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.

cool!
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:

image

Yep. It's a safe technique.

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

KC

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.

2 Likes

Thanks for sharing. Trying to get John's script working in KM as the speed difference isn't that important to me and I prefer the aesthetics of the Safari icon. I'm sure I'm missing something simple, but I've been unable to get John's script to execute by hotkey (though it will run when I go to KM and hit the run button). Can any you point out what I'm missing?

I have changed the standard CMD+Q shortcut to CMD+OPT+Q in the system keyboard shortcuts for Safari, so I can still quit without a dialog if I choose. Here is what I have so far in KM:

EDIT: For some additional context, I can run it by hotkey and have it just Quit without the dialog, but I can't seem to get it to run the script and then Quit or ignore based on the response to the prompt from the script.

Two things. In the script, removing the first two lines (each beginning with use) will likely help. (Alternatively, begin those two lines with two hyphens: --). Also, the Keyboard Maestro action to select the Menu item Quit is unnecessary since the AppleScript executes a command telling Safari to quit.

Thanks! I didn't catch those weren't commented out. I have now done so. I added the menu item action based on some troubleshooting earlier, but I have removed it now. I'm still unable to trigger the script by HotKey, but manually using the run button or the "Try" button produces the expected result. So does the macro look to be correctly configured now?

I'm on macOS 13.2 on an Apple Silicon Mac in case anyone knows of a bug or other reason that might be an issue. I've tried removing and reissuing the PPPC permissions. KM is working fine otherwise, so I doubt that was an issue. Unable to try ye olde reboot for a while due to a large task in process, but I'll rule that out as well when I get a chance.

Check the gear icon to the top right of your Execute AppleScript action to make certain nothing in there is selected that is preventing the macro from running.

Set to the defaults. Timeout is 99 hours. Not seeing anything that should be an issue, but I'm fairly inexperienced with KM.

Screenshot 2023-02-12 at 7.21.42 PM

Looks useful but judging from the ominous warning about 24 windows with 388 tabs this was from the bad old days when Safari didn't automatically open all the tabs that were open from last session. (Or all non-private ones, depending on the setting).

That usually happens when the Group is not enabled or is not available to the target app/window, so check the settings of your macro's Group.

I know you've now set up two key-chords, one for the dialog and one for insta-Quit, but here's a macro that will let you use one hotkey with either a short-press for the dialog or long-press (>0.5s, I think) for insta-Quit -- similar behaviour to Chrome's default. You can simply replace the "Alert" action with your AppleScript action.

Quit Test.kmmacros (5.6 KB)

Image

1 Like

I don't think so. That script was from 2020. The warning is produced by the script. Safari has had the feature to restore "All windows from last session" well before 2020. I see the value in the script for scenarios where you have active progress in a tab or window that won't be restored simply be reopening the site after the browser is closed. For example, if filling in a long web form that hasn't been submitted, or typing a comment or post that is lost if the window is closed.

1 Like

Thanks for sharing. I'll try that out. I appreciate your help.

EDIT: Bonus point @Nige_S! My macro group was scoped to Safari, but for kicks I decided to set it to a different app and then set it back to Safari just to rule it out. When the application selection menu opened, I had two instances of Safari and it was to use an alias. I selected the actual app instead of the alias and my macro worked as intended. That is my only active Safari macro, so I have a feeling that macro group was set up long ago (no idea how or why it was set to an alias though). Anyhow, thanks again!

Oh, and that AppleScript doesn't work as a drop-in replacement in your macro. When I tested it, the macro quits Safari, even if I select "cancel". Doesn't matter for my purposes, but FYI for anyone else who might want to go that route.

Post your actual macro -- not a screenshot -- so people can troubleshoot it. See here for how.

This is troubleshooting 101 for Keyboard Maestro – always try this when triggers aren't working.

Also – try running the Interactive Help from the Keyboard Maestro Editor's Help menu.

1 Like

A basic alternative for anyone who doesn't need anything sophisticated: Quit Safari, with confirmation check only if a window is open Macro

Sure. I have a working macro now, but here is your macro with the AppleScript substituted for the alert action, if you or anyone else wants to try it out.

Quit Test.kmmacros (7.0 KB)