@shadr/iterm2-ts 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Shad Reynolds
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,144 @@
1
+ # @shadr/iterm2-ts
2
+
3
+ TypeScript client library for iTerm2's native WebSocket/Protobuf API. Control iTerm2 programmatically from Node.js — no Python dependency required.
4
+
5
+ ## Requirements
6
+
7
+ - macOS
8
+ - iTerm2 with "Enable Python API" enabled (Preferences > General > Magic)
9
+ - Node.js 18+
10
+
11
+ ## Install
12
+
13
+ ```bash
14
+ npm install @shadr/iterm2-ts
15
+ ```
16
+
17
+ ## Quick Start
18
+
19
+ ```typescript
20
+ import { connect } from "@shadr/iterm2-ts";
21
+
22
+ const iterm = await connect();
23
+
24
+ try {
25
+ const app = await iterm.getApp();
26
+
27
+ // App → windows → tabs → sessions
28
+ const window = app.windows[0]; // { id, tabs, frame?, number? }
29
+ const tab = window.tabs[0]; // { id, sessions }
30
+ const session = tab.sessions[0]; // { id, name, frame?, gridSize? }
31
+
32
+ await iterm.sendText(session.id, "echo hello\n");
33
+ } finally {
34
+ iterm.disconnect();
35
+ }
36
+ ```
37
+
38
+ ## API Overview
39
+
40
+ ### Connection
41
+
42
+ ```typescript
43
+ import { connect } from "@shadr/iterm2-ts";
44
+
45
+ // Default connection
46
+ const iterm = await connect();
47
+
48
+ // With options — advisoryName identifies your script in iTerm2's API permissions dialog
49
+ const iterm = await connect({ advisoryName: "my-script" });
50
+ ```
51
+
52
+ ### Sessions
53
+
54
+ ```typescript
55
+ // Send keystrokes to a session
56
+ await iterm.sendText(session.id, "ls -la\n");
57
+
58
+ // Read visible screen contents as a string
59
+ const screen = await iterm.getScreenContents(session.id);
60
+
61
+ // Read scrollback buffer — returns an array of { text, hardNewline } per line
62
+ const lines = await iterm.getBuffer(session.id, 50);
63
+
64
+ // Split the session into two panes
65
+ const newSession = await iterm.splitPane(session.id, { direction: "horizontal" });
66
+ ```
67
+
68
+ ### Windows & Tabs
69
+
70
+ ```typescript
71
+ const { windowId, tabId, sessionId } = await iterm.createWindow();
72
+ const tab = await iterm.createTab(windowId);
73
+ await iterm.activate({ type: "session", id: sessionId });
74
+ await iterm.close({ type: "window", ids: [windowId] });
75
+ ```
76
+
77
+ ### Profiles
78
+
79
+ ```typescript
80
+ const profiles = await iterm.listProfiles();
81
+ const bgColor = await iterm.getProfileProperty(session.id, "Background Color");
82
+ await iterm.setProfileProperty(session.id, "Background Color", newColor);
83
+ ```
84
+
85
+ ### Events
86
+
87
+ ```typescript
88
+ // Callback — returns an unsubscribe function
89
+ const off = await iterm.on("sessionCreated", (event) => {
90
+ console.log("New session:", event.sessionId);
91
+ });
92
+
93
+ // Async iterator — blocks until the next event, good for loops
94
+ for await (const event of iterm.notifications("focusChanged")) {
95
+ console.log("Focus changed:", event);
96
+ }
97
+ ```
98
+
99
+ ### Cleanup
100
+
101
+ ```typescript
102
+ iterm.disconnect();
103
+ ```
104
+
105
+ ## Use Cases
106
+
107
+ - Automate dev environment setup (open windows, split panes, run commands)
108
+ - Build MCP servers that control iTerm2
109
+ - Script terminal layouts for presentations or demos
110
+ - Monitor session output programmatically
111
+ - React to terminal events (focus changes, keystrokes, new sessions)
112
+
113
+ ## Auth
114
+
115
+ The library authenticates with iTerm2 using a cookie-based system:
116
+
117
+ 1. Checks the `ITERM2_COOKIE` environment variable
118
+ 2. Falls back to requesting a cookie via `osascript` (AppleScript)
119
+
120
+ iTerm2 must be running with the Python API enabled.
121
+
122
+ ## Examples
123
+
124
+ See the [`examples/`](examples/) directory:
125
+
126
+ - [`list.ts`](examples/list.ts) — list all windows, tabs, sessions, and their buffer contents
127
+ - [`activate.ts`](examples/activate.ts) — focus a window, tab, or session by ID
128
+ - [`watch-events.ts`](examples/watch-events.ts) — subscribe to all events and log them
129
+
130
+ Run any example with:
131
+
132
+ ```bash
133
+ npx tsx examples/list.ts
134
+ ```
135
+
136
+ ## Documentation
137
+
138
+ - [Architecture](docs/architecture.md) — layer overview and design decisions
139
+ - [Protocol](docs/protocol.md) — wire protocol details and auth flow
140
+ - [API Reference](docs/api-reference.md) — all public methods with examples
141
+
142
+ ## License
143
+
144
+ MIT