Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Swift 3 script for sending files and folders to the trash, using the SwiftShell framework from https://github.com/kareman/SwiftShell .
#!/usr/bin/env swiftshell
/*
* Released under the MIT License (MIT), http://opensource.org/licenses/MIT
*/
import SwiftShell
import Dispatch
import Cocoa
extension Sequence where Iterator.Element: Hashable {
/// Returns an array containing each element in `self` only once, in the same order. Complexity: O(n)
func removeDuplicates () -> [Iterator.Element] {
var alreadyhere = Set<Iterator.Element>(minimumCapacity: underestimatedCount)
return filter { x in alreadyhere.contains(x) ? false : { alreadyhere.insert(x); return true }() }
}
}
DispatchQueue.main.async {
let filesToTrash = main.arguments.removeDuplicates().map(URL.init(fileURLWithPath:))
NSWorkspace.shared().recycle(filesToTrash) { trashedFiles, error in
guard let error = error else { exit(0) }
main.stderror.writeln("Files that could not be trashed:")
for file in filesToTrash where trashedFiles[file] == nil {
main.stderror.writeln(file.relativePath)
}
main.stderror.writeln()
exit(error)
}
}
RunLoop.current.run()
Owner

kareman commented Apr 12, 2016

See http://blog.nottoobadsoftware.com/swiftshell/move-files-to-the-trash/ for a brief description of this script.

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