Skip to content

Instantly share code, notes, and snippets.

@beccadax
Last active March 5, 2021 01:27
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save beccadax/4a48a5024dd01c1821b8 to your computer and use it in GitHub Desktop.
Save beccadax/4a48a5024dd01c1821b8 to your computer and use it in GitHub Desktop.
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
Copy link

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