From ed478be9835e984c42bc1e30fe84b5fb01a8c5d4 Mon Sep 17 00:00:00 2001 From: Ogulcan Celik Date: Wed, 20 May 2026 02:51:59 +0300 Subject: fix: bridge remote clipboard image paste refs #205 --- src/platform/macos.rs | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) (limited to 'src/platform/macos.rs') diff --git a/src/platform/macos.rs b/src/platform/macos.rs index f085ee91..d0cf43c8 100644 --- a/src/platform/macos.rs +++ b/src/platform/macos.rs @@ -5,7 +5,7 @@ use std::path::{Path, PathBuf}; use std::process::{Command, Stdio}; use std::sync::OnceLock; -use super::{ClipboardCommand, ForegroundJob, ForegroundProcess, Signal}; +use super::{ClipboardCommand, ClipboardImage, ForegroundJob, ForegroundProcess, Signal}; const PROC_PGRP_ONLY: u32 = 2; @@ -202,6 +202,47 @@ pub fn write_clipboard(bytes: &[u8]) -> bool { ) } +pub fn read_clipboard_image() -> Option { + let path = std::env::temp_dir().join(format!( + "herdr-clipboard-image-{}-{}.png", + std::process::id(), + unique_timestamp_nanos() + )); + let script = format!( + "set png_data to (the clipboard as «class PNGf»)\nset fp to open for access POSIX file \"{}\" with write permission\nwrite png_data to fp\nclose access fp", + path.display() + ); + + let status = Command::new("osascript") + .arg("-e") + .arg(script) + .stdin(Stdio::null()) + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .status() + .ok()?; + + if !status.success() { + let _ = std::fs::remove_file(&path); + return None; + } + + let bytes = std::fs::read(&path).ok(); + let _ = std::fs::remove_file(&path); + let bytes = bytes?; + (!bytes.is_empty()).then_some(ClipboardImage { + bytes, + extension: "png", + }) +} + +fn unique_timestamp_nanos() -> u128 { + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .map(|duration| duration.as_nanos()) + .unwrap_or(0) +} + /// Show a native macOS notification. /// /// Prefer `terminal-notifier` when it is installed because it can activate the -- cgit v1.2.3