{"record":{"id":"4f73314c4497b173","repo":"xai-org/grok-build","slug":"osascript-failed-stderr","errorCode":null,"errorMessage":"osascript failed: {stderr}","messagePattern":"osascript failed: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/codegen/xai-grok-shared/src/clipboard.rs","lineNumber":1185,"sourceCode":"        };\n        let path_str = path.display().to_string().replace('\"', \"\\\\\\\"\");\n        let script = format!(\n            \"set the clipboard to (read (POSIX file \\\"{path_str}\\\") as \\u{00AB}class {class}\\u{00BB})\"\n        );\n        let mut cmd = Command::new(\"osascript\");\n        cmd.arg(\"-e\")\n            .arg(&script)\n            .stdin(Stdio::null())\n            .stdout(Stdio::null())\n            .stderr(Stdio::piped());\n        xai_grok_tools::util::detach_std_command(&mut cmd);\n        let output = cmd\n            .output()\n            .map_err(|e| anyhow::anyhow!(\"failed to run osascript: {e}\"))?;\n\n        if !output.status.success() {\n            let stderr = String::from_utf8_lossy(&output.stderr);\n            anyhow::bail!(\"osascript failed: {stderr}\");\n        }\n        Ok(())\n    }\n}\n\n// ---------------------------------------------------------------------------\n// Linux / Windows: arboard with CLI-tool fallback on Linux\n// ---------------------------------------------------------------------------\n#[cfg(not(target_os = \"macos\"))]\nmod platform {\n    use super::ImageData;\n    use std::process::{Command, Stdio};\n\n    /// No subprocess-free pasteboard probe exists off-macOS.\n    pub(super) fn clipboard_image_snapshot() -> (Option<u64>, bool) {\n        (None, false)\n    }\n","sourceCodeStart":1167,"sourceCodeEnd":1203,"githubUrl":"https://github.com/xai-org/grok-build/blob/bc7f02eddd3d84085849dc19ed216f11c23b0571/crates/codegen/xai-grok-shared/src/clipboard.rs#L1167-L1203","documentation":"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.","triggerScenarios":"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).","commonSituations":"Copying screenshots that were deleted before the call; running from a sandboxed app without pasteboard entitlements; macOS privacy prompts denied for the host terminal.","solutions":["Verify the image file path exists and is readable before calling set_image_file","Read the stderr in the message for the osascript diagnostic and fix the script/permission issue","Grant the terminal app Automation permission (System Settings > Privacy & Security > Automation)","Test the osascript command manually to confirm the pasteboard is writable in the session"],"exampleFix":"// before\nclipboard::set_image_file(&missing_path)?;\n\n// after\nanyhow::ensure!(path.exists(), \"image not found: {}\", path.display());\nclipboard::set_image_file(&path)?;","handlingStrategy":"validation","validationCode":"fn ensure_image_readable(path: &std::path::Path) -> std::io::Result<()> {\n    let meta = std::fs::metadata(path)?;\n    if meta.len() == 0 {\n        return Err(std::io::Error::new(std::io::ErrorKind::InvalidData, \"empty image file\"));\n    }\n    std::fs::File::open(path)?.metadata()?;\n    Ok(())\n}","typeGuard":null,"tryCatchPattern":"match clipboard::set_image_file(&path) {\n    Err(e) if e.to_string().starts_with(\"osascript failed:\") => {\n        eprintln!(\"image copy failed: {e}\");\n        eprintln!(\"hint: grant the terminal Automation permission and verify the file exists\");\n    }\n    other => other?,\n}","preventionTips":["Check file existence/readability before calling set_image_file","Grant the host app Automation permission under macOS Privacy & Security","Test the osascript pasteboard script manually in the target session","Avoid deleting/clobbering screenshot files between capture and copy"],"tags":["clipboard","macos","osascript"],"backgroundTag":"clipboard-command-failed","analyzedSha":"bc7f02eddd3d84085849dc19ed216f11c23b0571","analyzedAt":"2026-08-31T04:59:42.031Z","schemaVersion":2},"datasetVersion":"2026-08-31T09:17:48.483Z"}