Win 转 Mac?一招找回“关闭即退出”的爽快

73 阅读2分钟

macOS 的“关闭窗口 ≠ 退出应用”逻辑,对于强迫症或者刚转系统的朋友来说真的很难受!😫 每次都要 Cmd+Q ,不然 Dock 栏永远一堆小白点。 作为一名喜欢折腾的前端工程师 👨‍💻,我写了一个 BetterTouchTool (BTT) 脚本,完美实现了**“点击红叉 = 退出应用”**,而且比网上的普通脚本更智能!

🚀 这个脚本厉害在哪?(逻辑详解)

  • 🤖 智能识别最后一个窗口:只有当那是最后一个窗口时才退出,多窗口时只关当前窗口。
  • 🛡️ 防误触保护:不仅判断窗口数量,还判断了窗口类型!如果你关的是“设置”、“关于”这种弹窗,绝对不会误杀退出整个 App 。
  • ✅ 常用软件白名单:像微信、网易云 这种需要挂后台的软件,脚本会自动放行,只关窗口不退后台!
  • ⚡️ 极速原生体验:底层模拟 Cmd+W ,没有任何延迟。

🛠️ BTT 设置教程:

-- 配置你的“保活”应用名单
set keepAliveApps to {"WeChat", "Telegram", "Spotify", "Music", "DingTalk"}

tell application "System Events"
    set frontApp to first application process whose frontmost is true
    set appName to name of frontApp
    
    -- 白名单检查:如果是常驻应用,只关闭不退出
    if keepAliveApps contains appName then
        keystroke "w" using command down
        return
    end if
    
    -- 获取窗口类型,防止误关设置弹窗
    try
        set currentSubrole to subrole of front window of frontApp
    on error
        set currentSubrole to "Unknown"
    end try
    
    set standardWindowCount to count of (windows of frontApp where subrole is "AXStandardWindow")
end tell

-- 逻辑判断
if currentSubrole is not "AXStandardWindow" then
    tell application "System Events" to keystroke "w" using command down
    return
end if

if standardWindowCount is less than or equal to 1 then
    tell application appName to quit
else
    tell application "System Events" to keystroke "w" using command down
end if