Prevent Accidental Safari Shutdowns

Say goodbye to accidental CMD+Q closes and take control of Safari’s quit command.

The Never-Ending Battle with CMD+Q in Safari

If you’re like me, you’ve experienced the frustration of accidentally closing Safari with a quick tap on CMD+Q. Unlike Chrome, which has a built-in option to require a longer key press, Safari just shuts down at the slightest hint of CMD+Q. After numerous accidental closes, I decided enough was enough—I needed a fix!

Enter Hammerspoon: The Key to Custom Mac Shortcuts

Hammerspoon is a powerful macOS automation tool that bridges Lua scripting with macOS controls, letting you create custom shortcuts and automate tasks. Think of it as the Swiss army knife of Mac shortcuts, allowing you to control specific applications, customize key combinations, and more.

Step 1: Installing Hammerspoon

To start, download and install Hammerspoon:

  1. Head to Hammerspoon’s official website and download the app.

    1. You can also do brew install --cask hammerspoon
  2. Install it by dragging it into the Applications folder.

  3. Open Hammerspoon and grant the necessary permissions (Hammerspoon will prompt you to do this).

  4. Once installed, click Reload Config from the Hammerspoon menu to load any new configurations.

Step 2: Setting Up the Script to Delay CMD+Q in Safari

Now we’ll add a script that intercepts CMD+Q specifically in Safari and prompts you for confirmation before closing.

  1. Open Hammerspoon and go to Open Config. This will open the init.lua file, where all your Hammerspoon scripts are stored.

  2. Copy and paste the following script into init.lua:

  3.   -- Hammerspoon script to confirm CMD+Q in Safari
      hs.hotkey.bind({"cmd"}, "Q", function()
          local app = hs.application.frontmostApplication()
          -- Check if the frontmost app is Safari
          if app:name() == "Safari" then
              local button, text = hs.dialog.blockAlert("Are you sure you want to quit Safari?", "Safari has open windows or tabs.", "Quit", "Cancel")
              if button == "Quit" then
                  app:kill() -- This command will close Safari if "Quit" is selected
              end
          else
              app:kill() -- Directly closes other apps without prompt
          end
      end)
    

Understanding the Code

  • hs.hotkey.bind({"cmd"}, "Q", function(): This line binds the CMD+Q key combination, intercepting it before macOS closes any app.

  • local app = hs.application.frontmostApplication(): This checks which app is currently active when CMD+Q is pressed.

  • if app:name() == "Safari" then: Only shows the confirmation dialog if Safari is the active app.

  • hs.dialog.blockAlert("Are you sure you want to quit Safari?", ...: Shows a dialog asking for confirmation.

  • app:kill(): If you choose “Quit,” Safari will close. If not, Safari remains open.

Step 3: Testing Your New CMD+Q Command

With the script in place, save and reload the configuration:

1. Go to the Hammerspoon menu in the top bar.

2. Select Reload Config.

3. Open Safari and try pressing CMD+Q. You should now see a confirmation dialog instead of Safari immediately closing.

The End of Accidental Closures

With Hammerspoon, I’ve regained control over my browser. This script has been a lifesaver, saving me countless accidental closures and much frustration.

Give it a try and see how it transforms your Safari experience!

Did you find this article valuable?

Support itheo.tech, I automate Sh!t by becoming a sponsor. Any amount is appreciated!