Skip to content

feat: use Hammerspoon URL scheme for click-to-focus - #7

Open
iFwu wants to merge 1 commit into
trentmcnitt:mainfrom
iFwu:feat/url-scheme-focus
Open

feat: use Hammerspoon URL scheme for click-to-focus#7
iFwu wants to merge 1 commit into
trentmcnitt:mainfrom
iFwu:feat/url-scheme-focus

Conversation

@iFwu

@iFwu iFwu commented Apr 1, 2026

Copy link
Copy Markdown

Summary

  • Fix shell escaping bug: The current create_focus_command builds a complex multi-line Lua script and passes it through shlex.quote()terminal-notifier -execute. The nested quoting (single quotes in Lua, shell escaping, terminal-notifier's own shell execution) causes the script to fail, triggering the "Could not restore window focus" error notification on every click.
  • Improve click-to-focus performance (~50%): Each notification click currently spawns a new hs CLI process with ~1.8s startup overhead, making total click-to-focus take ~3.4s. The URL scheme is handled in-process by Hammerspoon, reducing latency to ~1.6s (just the macOS Space switch animation).
  • Simplify code: Replaces 30+ lines of inline Lua script generation with a single URL string construction.

Changes

cc_notifier.py

  • Replace create_focus_command(window_id) -> list[str] with create_focus_url(window_id) -> str that returns a hammerspoon://focus?id=<window_id> URL
  • Change send_notification to use -open <url> instead of -execute <shell_command>
  • Remove import shlex (no longer needed)

README.md

  • Update Hammerspoon init.lua instructions: replace hs.window.filter + hs.timer requires with hs.urlevent and the focus URL event handler

Backward Compatibility

Fully backward compatible. Without Hammerspoon installed, focus_window_id is never set (the get_focused_window_id() call raises and sets window ID to "UNAVAILABLE"), so the -open code path is never reached.

Test plan

  • Verify notification appears correctly when user switches away from Claude Code window
  • Verify clicking notification focuses the correct window across macOS Spaces
  • Verify click-to-focus latency improvement (expected ~3.4s → ~1.6s)
  • Verify no "Could not restore window focus" error notifications appear
  • Verify behavior without Hammerspoon installed (graceful degradation, no -open flag sent)

@trentmcnitt

Copy link
Copy Markdown
Owner

I've been playing with this for the past 30 minutes and I can't get my window to focus when using the PR. Does cross-space window focusing work for you (including when you have multiple app windows open -- i.e. multiple VS Code projects in separate windows and spaces)?

What OS + Hammerspoon versions are you on?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants