1 、痛点:在 mac 上聊天的时候 iPhone 手机就一直咚咚的响好烦,如果点击顶部把 ”mac 已登录手机通知关闭“ 的话,又会忘了开,导致女神的消息无法及时回复,被 diss 不是一个合格的舔狗,emm...
2 、观察结论:如果在 mac 上退出微信的话,手机上的 ”mac 已登录手机通知关闭“ 会自动消掉,手机就能收到通知,从而达到无缝衔接的目的。
3 、解决思路:使用 hammerspoon 实现如下功能:a.每 30 秒检查一次键盘和鼠标动作,如果 120 秒没有动作就退出 mac 微信(注意不能用 killall ,否则 iPhone 那边不知道 mac 上已经退出了)b.当检测到有鼠标和键盘动作后,立即调起微信登录
4 、具体方法:a.下载、安装、打开 hammerspoon ( https://www.hammerspoon.org/)b.打开配置文件写入脚本:
`local idleLimit = 120 -- 120 秒无操作算不活跃local checkInterval = 30 -- 每 30 秒检查一次local wechatBundleID = "com.tencent.xinWeChat"
local timer = nillocal enabled = truelocal lastRunning = nil -- 记录上一次运行状态,避免重复日志
-- 判断微信是否运行local function isWeChatRunning()return hs.application.get(wechatBundleID) ~= nilend
-- 优雅退出微信local function quitWeChat(reason)local app = hs.application.get(wechatBundleID)if app thenapp:kill()hs.alert.show("💤 自动退出微信")print("WeChat quit (" .. (reason or "idle") .. ") at " .. os.date())lastRunning = falseendend
-- 启动微信local function launchWeChat(reason)if not enabled then return endif not isWeChatRunning() thenhs.application.launchOrFocusByBundleID(wechatBundleID)hs.alert.show("🚀 启动微信")print("WeChat launched (" .. (reason or "active") .. ") at " .. os.date())lastRunning = trueendend
-- 检查是否该退出local function checkIdle()if not enabled then return endlocal idleTime = hs.host.idleTime()local running = isWeChatRunning()
if running ~= lastRunning then print("WeChat running=" .. tostring(running) .. " at " .. os.date()) lastRunning = runningendif idleTime > idleLimit and running then quitWeChat("idle")endend
-- 电源事件监听:休眠时退出微信,唤醒后延迟启动local sleepWatcher = hs.caffeinate.watcher.new(function(eventType)if eventType == hs.caffeinate.watcher.systemWillSleep thenquitWeChat("sleep")elseif eventType == hs.caffeinate.watcher.systemDidWake thenhs.timer.doAfter(10, function()launchWeChat("wake")end)elseif eventType == hs.caffeinate.watcher.screensDidSleep thenquitWeChat("screens sleep")elseif eventType == hs.caffeinate.watcher.screensDidWake thenhs.timer.doAfter(5, function()launchWeChat("screens wake")end)endend)sleepWatcher:start()
-- 定时器:负责检测是否要退出if timer thentimer:stop()endtimer = hs.timer.doEvery(checkInterval, checkIdle)
-- 事件监听:键盘/鼠标动作 → 立即启动微信local eventtap = hs.eventtap.new({hs.eventtap.event.types.keyDown,hs.eventtap.event.types.mouseMoved,hs.eventtap.event.types.leftMouseDown,hs.eventtap.event.types.rightMouseDown}, function(_)launchWeChat("input")return falseend)eventtap:start()`后记:1 、在 iPhone 通知栏可设置 1 小时不提醒,今天不提醒,但是这样也是简单粗暴
2 、微信不像企业微信那样可以设置 5min 10min 无活动就恢复通知
在 AI 大爆发的时代,他们把东西做成这样我表示很痛心
