-
-
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 .
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See http://blog.nottoobadsoftware.com/swiftshell/move-files-to-the-trash/ for a brief description of this script.