From 9985ebd8558c2557adb6097f2abc2fbc39e88a12 Mon Sep 17 00:00:00 2001 From: ClSlaid Date: Mon, 23 Oct 2023 01:08:42 +0800 Subject: [PATCH] feat: add set files util for pasteboard binding Signed-off-by: ClSlaid --- src/pasteboard/mod.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/pasteboard/mod.rs b/src/pasteboard/mod.rs index 9765811b..4338fc26 100644 --- a/src/pasteboard/mod.rs +++ b/src/pasteboard/mod.rs @@ -111,4 +111,31 @@ impl Pasteboard { Ok(urls) } } + + /// Write a list of path to the pasteboard + pub fn set_files(&self, paths: &[&Path]) -> Result<(), Box> { + unsafe { + let urls: Vec = paths + .iter() + .map(|p| { + let url = format!("file://{}", p.display()); + NSURL::with_str(&url) + }) + .collect(); + let url_arr = NSArray::new(&urls); + + let _: id = msg_send![&*self.0, clearContents]; + let succ: bool = msg_send![&*self.0, writeObjects: &*url_arr]; + + if succ { + Ok(()) + } else { + return Err(Box::new(Error { + code: 666, + domain: "com.cacao-rs.pasteboard".to_string(), + description: "Pasteboard server set urls fail.".to_string() + })); + } + } + } }