iterm2-client
Rust client for the iTerm2 scripting API.
Communicates over WebSocket + Protobuf to control iTerm2 windows, tabs, and sessions programmatically. Covers all 34 operations in the iTerm2 API.
Quick start
use ;
async
Authentication
iTerm2 requires a cookie and key for API access. The client resolves credentials in order:
ITERM2_COOKIEandITERM2_KEYenvironment variables- AppleScript request to iTerm2 via
osascript(prompts user for permission on first use)
Architecture
Two-layer API
Low-level -- Connection::call() sends raw protobuf ClientOriginatedMessage and returns ServerOriginatedMessage. Use this for operations not covered by the high-level API, or when you need full control over the request/response.
use ;
let conn = connect.await?;
let resp = conn.call.await?;
High-level -- App, Window, Tab, and Session types wrap Arc<Connection> and provide ergonomic methods with status checking.
use ;
let app = new;
let session = &app.list_sessions.await?.windows.tabs.sessions;
session.send_text.await?;
let lines = session.get_screen_contents.await?;
Notifications
Subscribe to iTerm2 events (new sessions, keystrokes, focus changes, etc.):
use ;
use StreamExt;
// Subscribe at the protocol level
conn.call.await?;
// Receive typed notifications
let rx = conn.subscribe_notifications;
let mut stream = Boxpin;
while let Some = stream.next.await
API coverage
All 34 iTerm2 API operations have request builders in the request module:
| Category | Operations |
|---|---|
| Sessions | list_sessions, send_text, get_buffer, get_prompt, list_prompts, split_pane, inject, restart_session, close_sessions |
| Tabs | create_tab, close_tabs, set_tab_layout, reorder_tabs |
| Windows | close_windows, get_property, set_property |
| Activation | activate_session, activate_tab, activate_window, activate_app |
| Variables | get_variable_session, set_variable_session, get_variable_tab, get_variable_window, get_variable_app |
| Profiles | get_profile_property, set_profile_property, list_profiles |
| Notifications | subscribe_notification, unsubscribe_notification |
| Other | focus, begin_transaction, end_transaction, saved_arrangement, color_presets, broadcast_domains, tmux, preferences, register_tool, invoke_function, selection, menu_item, status_bar_component, rpc_result |
Testing
# Unit tests (mock WebSocket server, no iTerm2 needed)
# Integration tests (requires running iTerm2)
# Coverage report (requires cargo-llvm-cov)
License
GPL-2.0-only (matching iTerm2)