-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moves files to trash on macOS by executing an AppleScript command to delete all requested paths.
- Loading branch information
Showing
1 changed file
with
47 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,63 @@ | ||
|
||
use std::ffi::OsString; | ||
use std::path::Path; | ||
use std::process::Command; | ||
|
||
use crate::Error; | ||
|
||
pub fn is_implemented() -> bool { | ||
false | ||
true | ||
} | ||
|
||
pub fn remove_all<I, T>(paths: I) -> Result<(), Error> | ||
where | ||
I: IntoIterator<Item = T>, | ||
T: AsRef<Path>, | ||
{ | ||
unimplemented!(); | ||
let full_paths = paths | ||
.into_iter() | ||
// Convert paths into canonical, absolute forms and collect errors | ||
.map(|path| { | ||
path.as_ref() | ||
.canonicalize() | ||
.map_err(|e| Error::CanonicalizePath { | ||
code: e.raw_os_error(), | ||
}) | ||
}) | ||
// Convert paths into &strs and collect errors | ||
.map(|path| path.and_then(|p| p.to_str().ok_or(Error::Unknown).map(|s| s.to_owned()))) | ||
.collect::<Result<Vec<String>, Error>>()?; | ||
|
||
// AppleScript command to move files (or directories) to Trash looks like | ||
// osascript -e 'tell application "Finder" to delete { POSIX file "file1", POSIX "file2" }' | ||
// The `-e` flag is used to execute only one line of AppleScript. | ||
let mut command = Command::new("osascript"); | ||
let posix_files = full_paths | ||
.into_iter() | ||
.map(|p| format!("POSIX file \"{}\"", p)) | ||
.collect::<Vec<String>>() | ||
.join(", "); | ||
let script = format!( | ||
"tell application \"Finder\" to delete {{ {} }}", | ||
posix_files | ||
); | ||
|
||
let argv: Vec<OsString> = vec!["-e".into(), script.into()]; | ||
command.args(argv); | ||
|
||
// Execute command | ||
let result = command.output().map_err(|e| Error::Remove { | ||
code: e.raw_os_error(), | ||
})?; | ||
|
||
if !result.status.success() { | ||
return Err(Error::Remove { | ||
code: result.status.code(), | ||
}); | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
pub fn remove<T: AsRef<Path>>(path: T) -> Result<(), Error> { | ||
unimplemented!(); | ||
remove_all(&[path]) | ||
} |