公司动态

AI 写完了代码,你却还在发呆?用 Claude Code Hooks 给 AI 装上“下班铃“

📅 2026/7/23 18:25:52
AI 写完了代码,你却还在发呆?用 Claude Code Hooks 给 AI 装上“下班铃“
AI 写完了代码你却还在发呆用 Claude Code Hooks 给 AI 装上下班铃你有没有过这种经历让 AI 帮你重构一个模块然后切到浏览器摸鱼结果十分钟后切回来发现他早就干完了——而你白白浪费了宝贵的摸鱼时间今天就来教你怎么让 AI 干活结束时自动弹窗喊你。一、痛点场景用 Claude Code或类似的 AI 编程工具执行长任务时通常的模式是下指令 → 2. 干等 → 3. 时不时回来看看 → 4. 发现早完成了这个时不时回来看看就是效率黑洞。作为一个追求极致摸鱼效率的人我翻了 Claude Code 的文档找到了Hooks机制。二、Claude Code Hooks 是什么Hooks 是 Claude Code 提供的生命周期钩子你可以在特定事件触发时自动执行自定义脚本。支持的事件包括Hook 事件触发时机PreToolUse工具调用前PostToolUse工具调用后Notification通知事件StopAgent 任务结束PreCompact上下文压缩前其中最实用的就是Stop——任务完成时触发正是我们要的下班铃。三、配置文件结构Hooks 配置存放在.claude/settings.json中{hooks:{Stop:[{hooks:[{type:command,command:powershell.exe -ExecutionPolicy Bypass -File \path\\to\\hook\\notify.ps1\ -Message \任务已结束\}]}]}}配置说明hooks.Stop— 数组可注册多个 Stop hook每个 hook 内再嵌套一个hooks数组这是command类型command字段指定要执行的脚本支持传参四、Windows 弹窗通知脚本光执行脚本还不够——我们要一个能弹窗播放音效的体验。这里用 PowerShell 调用 Windows Forms 写了一个通知脚本notify.ps1param([string]$Message任务完成,[string]$TitleClaude Code,[string]$SoundExclamation,[string]$SoundFile)Add-Type-AssemblyName System.Windows.FormsAdd-Type-AssemblyName System.Drawing# 音效播放 functionPlay-Sound{param([string]$SoundType,[string]$FilePath)if($FilePath-and(Test-Path$FilePath)){try{(New-ObjectSystem.Media.SoundPlayer($FilePath)).Play()return}catch{}}if($SoundType-eqNone){return}$map {Beep[System.Media.SystemSounds]::BeepExclamation[System.Media.SystemSounds]::ExclamationQuestion[System.Media.SystemSounds]::QuestionAsterisk[System.Media.SystemSounds]::Asterisk}if($map.ContainsKey($SoundType)){$map[$SoundType].Play()}}Play-Sound-SoundType$Sound-FilePath$SoundFile# GUI 弹窗暗黑风格 $formNew-ObjectSystem.Windows.Forms.Form$form.Text $Title$form.Size New-ObjectSystem.Drawing.Size(400,200)$form.StartPosition CenterScreen$form.BackColor #2d2d30$labelNew-ObjectSystem.Windows.Forms.Label$label.Text $Message$label.Font New-ObjectSystem.Drawing.Font(Microsoft YaHei,16)$label.ForeColor #ffffff$label.AutoSize $true$label.Location New-ObjectSystem.Drawing.Point(50,50)$buttonNew-ObjectSystem.Windows.Forms.Button$button.Text 确定$button.Size New-ObjectSystem.Drawing.Size(80,30)$button.Location New-ObjectSystem.Drawing.Point(160,100)$button.Add_Click({$form.Close()})$form.Controls.Add($label)$form.Controls.Add($button)$form.ShowDialog()|Out-Null效果AI 完成任务 → 系统发出提示音 → 屏幕中央弹出深色主题弹窗显示自定义消息点击确定关闭。五、权限白名单当你使用 Hooks 执行脚本时Claude Code 会弹出权限确认。如果你不想每次都点确认可以在.claude/settings.local.json里加上白名单{permissions:{allow:[Bash(powershell.exe *),Bash(npm -v),Bash(pnpm install *)]}}这样 Hook 脚本就能静默执行不会在关键时刻打断 AI 的工作流。六、进阶玩法不止于提醒Hooks 的潜力远不止弹窗通知。你可以用它来做PreToolUse→ AI 每次操作前记录日志方便审计PostToolUse→ 自动eslint --fix格式化 AI 修改过的文件Stop→ 完成时自动git diff生成变更摘要、自动运行测试用例Notification→ 集成飞书 / 钉钉 / 企业微信 Webhook实现远程通知一个稍微复杂一点的 Stop Hook 示例{hooks:{Stop:[{hooks:[{type:command,command:powershell -File notify.ps1 -Message 代码生成完毕},{type:command,command:npm run lint}]}]}}这样任务结束后先弹窗提醒再自动跑 Lint 检查。七、跨平台兼容macOS / Linux 用户可以用osascript或notify-send实现类似效果macOS:{command:osascript -e display notification \任务完成\ with title \Claude Code\ sound name \Glass\}Linux:{command:notify-send Claude Code 任务已完成 -i dialog-information}八、总结Claude Code Hooks 让AI 写代码你摸鱼的模式真正闭环了。配置一次受益终身。核心要点Hook 配置写在.claude/settings.json支持多种生命周期事件Stop Hook 最实用——任务完成时自动通知跨平台兼容——PowerShell / osascript / notify-send 都能接不止通知——可以串联 lint、测试、构建等自动化流程希望这篇文章能帮你在摸鱼之路上更进一步。如果你有其他有趣的 Hooks 玩法欢迎在评论区交流。*本文示例代码基于 Windows 11 PowerShell 5.1 Claude Code 实践。