Last active
March 5, 2021 01:27
-
-
Save beccadax/4a48a5024dd01c1821b8 to your computer and use it in GitHub Desktop.
A trash command as a Swift script.
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
#!/usr/bin/xcrun swift | |
import Dispatch | |
import Cocoa | |
dispatch_async(dispatch_get_main_queue()) { | |
let URLsToRecycle = Array(dropFirst(Process.arguments)).map { NSURL(fileURLWithPath: $0) } | |
NSWorkspace.sharedWorkspace().recycleURLs(URLsToRecycle) { dict, error in | |
let recycledURLs = Array(dict.keys) as [NSURL] | |
if error != nil { | |
// Couldn't make this work, sadly—compiler crash. | |
// var errstream = NSFileHandle.fileHandleWithStandardError() | |
println("\(error.localizedDescription)"/*, &errstream*/) | |
println(""/*, &errstream*/) | |
println("Files that were not trashed:"/*, &errstream*/) | |
for URL in URLsToRecycle { | |
if !contains(recycledURLs, URL) { | |
println("\(URL.path)"/*, &errstream*/) | |
} | |
} | |
exit(1) | |
} | |
else { | |
exit(0) | |
} | |
} | |
} | |
NSRunLoop.currentRunLoop().run() | |
// extension NSFileHandle: OutputStreamType { | |
// public func write(str: String) { | |
// let data = str.dataUsingEncoding(NSUTF8StringEncoding) | |
// self.writeData(data) | |
// } | |
// } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Excellent, I really don't like using "rm" on the command line. With this script I can at least undo my mistakes.
I made a version of this at https://gist.github.com/kareman/322c1091f3cc7e1078af using the SwiftShell framework.