xai-org/grok-build · error

osascript failed: {stderr}

Error message

osascript failed: {stderr}

What it means

The macOS image-copy path (`set_image_file`) runs an osascript snippet that loads a file into the pasteboard; if osascript exits non-zero, the leg fails with this message containing osascript's stderr, e.g. syntax errors, permission denials, or missing file paths.

Source

Thrown at crates/codegen/xai-grok-shared/src/clipboard.rs:1185

        };
        let path_str = path.display().to_string().replace('"', "\\\"");
        let script = format!(
            "set the clipboard to (read (POSIX file \"{path_str}\") as \u{00AB}class {class}\u{00BB})"
        );
        let mut cmd = Command::new("osascript");
        cmd.arg("-e")
            .arg(&script)
            .stdin(Stdio::null())
            .stdout(Stdio::null())
            .stderr(Stdio::piped());
        xai_grok_tools::util::detach_std_command(&mut cmd);
        let output = cmd
            .output()
            .map_err(|e| anyhow::anyhow!("failed to run osascript: {e}"))?;

        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            anyhow::bail!("osascript failed: {stderr}");
        }
        Ok(())
    }
}

// ---------------------------------------------------------------------------
// Linux / Windows: arboard with CLI-tool fallback on Linux
// ---------------------------------------------------------------------------
#[cfg(not(target_os = "macos"))]
mod platform {
    use super::ImageData;
    use std::process::{Command, Stdio};

    /// No subprocess-free pasteboard probe exists off-macOS.
    pub(super) fn clipboard_image_snapshot() -> (Option<u64>, bool) {
        (None, false)
    }

View on GitHub (pinned to bc7f02eddd)

Solutions

  1. Verify the image file path exists and is readable before calling set_image_file
  2. Read the stderr in the message for the osascript diagnostic and fix the script/permission issue
  3. Grant the terminal app Automation permission (System Settings > Privacy & Security > Automation)
  4. Test the osascript command manually to confirm the pasteboard is writable in the session

Example fix

// before
clipboard::set_image_file(&missing_path)?;

// after
anyhow::ensure!(path.exists(), "image not found: {}", path.display());
clipboard::set_image_file(&path)?;
Defensive patterns

Strategy: validation

Validate before calling

fn ensure_image_readable(path: &std::path::Path) -> std::io::Result<()> {
    let meta = std::fs::metadata(path)?;
    if meta.len() == 0 {
        return Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "empty image file"));
    }
    std::fs::File::open(path)?.metadata()?;
    Ok(())
}

Try / catch

match clipboard::set_image_file(&path) {
    Err(e) if e.to_string().starts_with("osascript failed:") => {
        eprintln!("image copy failed: {e}");
        eprintln!("hint: grant the terminal Automation permission and verify the file exists");
    }
    other => other?,
}

Prevention

When it happens

Trigger: Calling set_image_file with a path that doesn't exist or isn't readable, osascript denied Automation/Finder access by TCC, or the AppleScript failing at runtime (non-zero exit).

Common situations: Copying screenshots that were deleted before the call; running from a sandboxed app without pasteboard entitlements; macOS privacy prompts denied for the host terminal.

Related errors


AI-assisted analysis of xai-org/grok-build@bc7f02eddd (2026-08-31). Data as JSON: /api/errors/4f73314c4497b173. Report an issue: GitHub.