8000 GitHub - dra11y/iterm2-api: A Rust API to programmatically access iTerm2's Python API directly over Unix domain socket without Python
[go: up one dir, main page]

Skip to content

A Rust API to programmatically access iTerm2's Python API directly over Unix domain socket without Python

Notifications You must be signed in to change notification settings

dra11y/iterm2-api

Repository files navigation

iterm2-api

A Rust library for programmatically controlling iTerm2 via its official API over Unix domain socket without Python.

Prerequisites

Before using this library, you must enable iTerm2's API server and grant permission:

  1. Open iTerm2
  2. Go to Settings > General > Magic
  3. Check "Enable Python API"
  4. Optional: To avoid a permission prompt every time you run your helper app, change "Require 'Automation' permission" to "Allow all apps to connect"

Basic Usage

use iterm2_api::ITerm2Connection;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Connect to iTerm2
    let mut connection = ITerm2Connection::connect().await?;

    // Create a new tab (creates a new window)
    let session = connection.create_tab(None, None).await?;

    // Send a command to the new session
    connection.send_text(session.unique_identifier(), "echo Hello World\r").await?;

    Ok(())
}

Examples

Advanced Tab Management

use iterm2_api::ITerm2Connection;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut connection = ITerm2Connection::connect().await?;

    // Create first tab to establish a window
    let first_session = connection.create_tab(None, None).await?;
    let windows = connection.get_windows().await?;
    let window_id = windows.first().unwrap().window_id().to_string();

    // Create 3 more tabs in the same window
    let work_dir = "/tmp";
    let mut sessions = vec![first_session.unique_identifier().to_string()];

    for i in 2..=4 {
        let session = connection.create_tab(None, Some(&window_id)).await?;
        let session_id = session.unique_identifier().to_string();

        // Change to working directory
        let cd_command = format!("cd {}\r", work_dir);
        connection.send_text(&session_id, &cd_command).await?;

        sessions.push(session_id);
    }

    // Run commands in specific tabs
    for session_id in sessions.iter().take(2) {
        connection.send_text(session_id, "ls -la\r").await?;
    }

    Ok(())
}

See the examples/ directory for more comprehensive usage examples.

About

A Rust API to programmatically access iTerm2's Python API directly over Unix domain socket without Python

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published
0