Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
A trash command as a Swift script.
#!/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)
// }
// }

kareman commented Oct 24, 2014

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment