Skip to content

fix: apply iTerm2 terminal colors via AppleScript from Claude Code hook subprocesses - #2

Open
herrsergio wants to merge 3 commits into
basicScandal:mainfrom
herrsergio:fix/iterm2-colors-applescript
Open

fix: apply iTerm2 terminal colors via AppleScript from Claude Code hook subprocesses#2
herrsergio wants to merge 3 commits into
basicScandal:mainfrom
herrsergio:fix/iterm2-colors-applescript

Conversation

@herrsergio

Copy link
Copy Markdown

Summary

  • Replace OSC escape sequence approach with AppleScript (osascript) for iTerm2 color theming
  • Fix skin state being cleared on every SessionEnd — now only cleared on explicit /skin reset
  • Pass --reset flag from skin-command-hook.sh to deactivate.sh to distinguish the two cases

Problem

Claude Code hook subprocesses run without a TTY attached, breaking two standard approaches:

  • /dev/tty — not available in hook subprocesses (Device not configured)
  • OSC sequences via stdout — Claude Code's TUI renderer strips the leading ESC byte before output reaches the terminal emulator, so sequences arrive as plain text and are ignored

Solution

osascript communicates with iTerm2 via Apple Events, bypassing the PTY entirely. It works from any subprocess regardless of TTY availability and applies full color control: background, foreground, cursor, and the complete ANSI 0–7 palette.

iTerm2's AppleScript API uses 16-bit color values (0–65535). Hex colors convert as channel × 257.

On non-iTerm2 terminals all osascript calls fail silently (2>/dev/null || true) — no errors, no color changes.

Files changed

  • engine/activate.sh — replace OSC block with hex_to_16bit + iterm_set_color helpers using AppleScript
  • engine/deactivate.sh — replace OSC reset block with AppleScript; only write default to current when --reset flag is passed
  • engine/skin-command-hook.sh — pass --reset to deactivate.sh on /skin reset

Testing

  1. Open Claude Code in iTerm2
  2. Run /skin mythos — background should shift to deep navy (#0D1B3D) immediately
  3. Run /skin retro86 — background should shift to deep purple (#40318D)
  4. Run /skin reset — background should restore to black

To test the no-TTY path directly:

osascript -e 'tell application "iTerm2" to tell current session of current window to set background color to {13107, 27499, 61166}'

Known limitations

  • macOS + iTerm2 only. Other terminal emulators are unaffected (silent no-op).
  • Cursor color may be overridden by iTerm2 profile settings if "Use separate colors for light and dark mode" is enabled. Workaround: disable that option in Profiles → Colors.
  • Restore values in deactivate.sh are a generic default palette. Users with non-standard default profiles should adjust those values to match their iTerm2 profile.

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.

1 participant