在 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 = nil
local enabled = true
local lastRunning = nil -- 记录上一次运行状态,避免重复日志
-- 判断微信是否运行
local function isWeChatRunning()
return hs.application.get(wechatBundleID) ~= nil
end
-- 优雅退出微信
local function quitWeChat(reason)
local app = hs.application.get(wechatBundleID)
if app then
app:kill()
hs.alert.show("💤 自动退出微信")
print("WeChat quit (" .. (reason or "idle") .. ") at " .. os.date())
lastRunning = false
end
end
-- 启动微信
local function launchWeChat(reason)
if not enabled then return end
if not isWeChatRunning() then
hs.application.launchOrFocusByBundleID(wechatBundleID)
hs.alert.show("🚀 启动微信")
print("WeChat launched (" .. (reason or "active") .. ") at " .. os.date())
lastRunning = true
end
end
-- 检查是否该退出
local function checkIdle()
if not enabled then return end
local idleTime = hs.host.idleTime()
local running = isWeChatRunning()
if running ~= lastRunning then
print("WeChat running=" .. tostring(running) .. " at " .. os.date())
lastRunning = running
end
if idleTime > idleLimit and running then
quitWeChat("idle")
end
end
-- 电源事件监听:休眠时退出微信,唤醒后延迟启动
local sleepWatcher = hs.caffeinate.watcher.new(function(eventType)
if eventType == hs.caffeinate.watcher.systemWillSleep then
quitWeChat("sleep")
elseif eventType == hs.caffeinate.watcher.systemDidWake then
hs.timer.doAfter(10, function()
launchWeChat("wake")
end)
elseif eventType == hs.caffeinate.watcher.screensDidSleep then
quitWeChat("screens sleep")
elseif eventType == hs.caffeinate.watcher.screensDidWake then
hs.timer.doAfter(5, function()
launchWeChat("screens wake")
end)
end
end)
sleepWatcher:start()
-- 定时器:负责检测是否要退出
if timer then
timer:stop()
end
timer = 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 false
end)
eventtap:start()
`
后记:
1 、在 iPhone 通知栏可设置 1 小时不提醒,今天不提醒,但是这样也是简单粗暴
2 、微信不像企业微信那样可以设置 5min 10min 无活动就恢复通知
在 AI 大爆发的时代,他们把东西做成这样我表示很痛心
hammerspoon, 微信, 自动化