Skip to content

Commit

Permalink
feat: add set files util for pasteboard binding
Browse files Browse the repository at this point in the history
Signed-off-by: ClSlaid <[email protected]>
  • Loading branch information
ClSlaid committed Oct 22, 2023
1 parent 0f64a84 commit 9985ebd
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/pasteboard/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn std::error::Error>> {
unsafe {
let urls: Vec<NSURL> = 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()
}));
}
}
}
}

0 comments on commit 9985ebd

Please sign in to comment.