Photo by Rubaitul Azad on Unsplash
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:
Head to Hammerspoon’s official website and download the app.
- You can also do
brew install --cask hammerspoon
- You can also do
Install it by dragging it into the Applications folder.
Open Hammerspoon and grant the necessary permissions (Hammerspoon will prompt you to do this).
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.
Open Hammerspoon and go to Open Config. This will open the init.lua file, where all your Hammerspoon scripts are stored.
Copy and paste the following script into init.lua:
-- 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!