Skip to content

Instantly share code, notes, and snippets.

@kareman
Forked from beccadax/trash.swift
Last active April 14, 2020 13:40
Show Gist options
  • Save kareman/322c1091f3cc7e1078af to your computer and use it in GitHub Desktop.
Save kareman/322c1091f3cc7e1078af to your computer and use it in GitHub Desktop.
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()
@kareman
Copy link
Author

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