0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ClaudeCodeの入力待ちをデスクトップ通知で知らせる方法

0
Posted at

はじめに

Claude Codeを開発に利用しています。プロンプトを投げたターミナルから離れ、別の作業をしてターミナルに戻ってくると、「入力待ちで止まってた!」なんてこともしばしば...

最近見たBoris Cherny氏のClaude Codeセットアップ方法を読んでいて、OSに通知を飛ばせることを知りました。
今回はClaude Code DocsとHooks referenceを参考に、OS通知をセットアップした手順をまとめていきます。

環境
パソコン: macOS
ターミナル: iTerm2

🔧 Setup

bashコマンドの実行ログを記録するHookの設定

まずClaude Code Docsの通りにLogの出力Hookをセットしました。(Claude Code Docsと同じ手順なので、すでにhooksのセットをしてある方はスキップでもOKです)

BashコマンドLog hookの設定方法

準備. jqを確認する

jqコマンドがあるか確認します。今回は入っていたので飛ばしますが、ない場合はインストールしてください。

$ jq
jq - commandline JSON processor [version 1.7.1-apple]

Usage:	jq [options] <jq filter> [file...]
	jq [options] --args <jq filter> [strings...]
	jq [options] --jsonargs <jq filter> [JSON_TEXTS...]

jq is a tool for processing JSON inputs, applying the given filter to
its JSON text inputs and producing the filter's results as JSON on
standard output.

The simplest filter is ., which copies jq's input to its output
unmodified except for formatting. For more advanced filters see
the jq(1) manpage ("man jq") and/or https://jqlang.github.io/jq/.

Example:

	$ echo '{"foo": 0}' | jq .
	{
	  "foo": 0
	}

For listing the command options, use jq --help.

Step1. hook設定を始める

# Claude Codeを起動
claude

#### hooksを設定していく
/hooks
# 1. PreToolUseを選択
 ❯ 1.  PreToolUse - Before tool execution
   2.  PostToolUse - After tool execution
   3.  PostToolUseFailure - After tool execution fails
   4.  Notification - When notifications are sent
 ↓ 5.  UserPromptSubmit - When the user submits a prompt

Step2. matcherを追加する

### Add new matcherを選択 (そのままEnterでok)
   PreToolUse - Tool Matchers
 Input to command is JSON of tool call arguments.
 Exit code 0 - stdout/stderr not shown
 Exit code 2 - show stderr to model and block tool call
 Other exit codes - show stderr to user only but continue with tool call

 ❯ 1. + Add new matcher…
   No matchers configured yet


### Bash を入力する
   Add new matcher for PreToolUse
 Input to command is JSON of tool call arguments.
 Exit code 0 - stdout/stderr not shown
 Exit code 2 - show stderr to model and block tool call
 Other exit codes - show stderr to user only but continue with tool call

 Possible matcher values for field tool_name:

 Task, TaskOutput, Bash, Glob, Grep, ExitPlanMode, Read, Edit, Write, NotebookEdit, WebFetch, TodoWrite, WebSearch, KillShell, AskUserQuestion, Skill, EnterPlanMode, ToolSearch

 Tool matcher:
 ╭──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
 │ Bash                                                                                                                                                                                                         │
 ╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯

 Example Matchers:
 • Write (single tool)
 • Write|Edit (multiple tools)
 • Web.* (regex pattern)

 Enter to confirm · esc to cancel

Step3. hookを追加する

実行するコマンド: jq -r '"\(.tool_input.command) - \(.tool_input.description // "No description")"' >> ~/.claude/bash-command-log.txt

 ### Add new hookが出てくるので、Enter
  PreToolUse - Matcher: Bash
 Input to command is JSON of tool call arguments.
 Exit code 0 - stdout/stderr not shown
 Exit code 2 - show stderr to model and block tool call
 Other exit codes - show stderr to user only but continue with tool call

 ❯ 1. + Add new hook…
   No hooks configured yet

 Enter to confirm · esc to cancel


 ### 実行コマンドを入力する
   Event: PreToolUse - Before tool execution

 Input to command is JSON of tool call arguments.
 Exit code 0 - stdout/stderr not shown
 Exit code 2 - show stderr to model and block tool call
 Other exit codes - show stderr to user only but continue with tool call

 Matcher: Bash

 Command:

 ╭──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
 │ jq -r '"\(.tool_input.command) - \(.tool_input.description // "No description")"' >> ~/.claude/bash-command-log.txt                                                                                          │
 ╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯

 Examples:
 • jq -r '.tool_input.file_path | select(endswith(".go"))' | xargs -r gofmt -w
 • jq -r '"\(.tool_input.command) - \(.tool_input.description // "No description")"' >> ~/.claude/bash-command-log.txt
 • /usr/local/bin/security_check.sh
 • python3 ~/hooks/validate_changes.py

 Enter to confirm · esc to cancel

Step4. 設定を保存する

 ### User settingsを選択
 Save hook configuration

   Event: PreToolUse - Before tool execution
   Matcher: Bash
   Command: jq -r '"\(.tool_input.command) - \(.tool_input.description // "No description")"' >> ~/.claude/bash-command-log.txt

 Where should this hook be saved?
# 3. User settingsを選択
 ❯ 1. Project settings (local)  Saved in .claude/settings.local.json
   2. Project settings          Checked in at .claude/settings.json
   3. User settings             Saved in at ~/.claude/settings.json

 Enter to confirm · esc to cancel

対話モードに戻るまで(/hooksから抜けるまで)escボタンを押す

Step5. hookを確認する


### 別ターミナルでsettings.jsonを確認
 vim  ~/.claude/settings.json
 {
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "hooks": [
          {
            "type": "command",
            "command": "jq -r '\"\\(.tool_input.command) - \\(.tool_input.description // \"No description\")\"' >> ~/.claude/bash-command-log.txt"
          }
        ]
      }
    ]
  }
}

Step6. hookのテスト

1. 現在のディレクトリをlsして などシンプルなプロンプトを実行してもらう
2. cat ~/.claude/bash-command-log.txtでログを確認する
ls - List files in current directory が出ていればOK

Setup 入力待ち通知 hook

ここから本題の入力待ち通知のhookをセットしていきます。
settings.jsonにNotificationを追加し、OSに通知を出すためのコマンドと条件を追加しました。

vim ~/.claude/settings.json

{
  "hooks": {
    "PreToolUse": [
      ##...既存の設定
    ],
    #### Notification 追加 ####
   "Notification": [
      {
        "matcher": "permission_prompt",
        "hooks": [
          {
            "type": "command",
            "command": "osascript -e 'display notification \"Awaiting your input\" with title \"Claude Code\"'"
          }
        ]
      }
    ]
  }
}

matcherは4種類ありますが、今回はファイル編集や読み取り関係の yes/noの入力待ちで通知してほしかったので permission_promptで通知が来るよう設定しています。

テスト

ファイルの新規作成をClaude Codeに頼んでみます。

❯ このディレクトリに新しくtest.txtを作成して

# (中略)
 Do you want to create test.txt?
 ❯ 1. Yes
   2. Yes, allow all edits during this session (shift+tab)
   3. No

通知きた! 🥳
image.png

通知が来ない時

macの設定で、iTermの通知を許可していなかったので最初通知が来なくて焦りました...
システム設定 > 通知 > iTermから"通知を許可"にする必要があります。
image.png

まとめ

これでClaudeにたくさん仕事を任せられますね!
Plan modeも気になっているので、今後どんどん試してみたいと思います。

参考になれば幸いです :apple:

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?