Skip to content

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*/)
else {
// extension NSFileHandle: OutputStreamType {
// public func write(str: String) {
// let data = str.dataUsingEncoding(NSUTF8StringEncoding)
// self.writeData(data)
// }
// }

This comment has been minimized.

Copy link

@kareman 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 using the SwiftShell framework.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.