Instantly share code, notes, and snippets.

@kareman kareman/trash.swift forked from brentdax/trash.swift
Last active Oct 9, 2016

What would you like to do?
Swift 3 script for sending files and folders to the trash, using the SwiftShell framework from .
#!/usr/bin/env swiftshell
* Released under the MIT License (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 {

This comment has been minimized.

Copy link
Owner Author

kareman commented Apr 12, 2016

See 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