├── .editorconfig ├── .gitattributes ├── .gitignore ├── Package.swift ├── Sources └── trash │ ├── Utilities.swift │ └── main.swift ├── build ├── license └── readme.md /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /.build 2 | /*.xcodeproj 3 | /trash 4 | /.swiftpm 5 | -------------------------------------------------------------------------------- /Package.swift: -------------------------------------------------------------------------------- 1 | // swift-tools-version:5.11 2 | import PackageDescription 3 | 4 | let package = Package( 5 | name: "trash", 6 | platforms: [ 7 | .macOS(.v10_13) 8 | ], 9 | products: [ 10 | .executable( 11 | name: "trash", 12 | targets: [ 13 | "trash" 14 | ] 15 | ) 16 | ], 17 | targets: [ 18 | .executableTarget(name: "trash") 19 | ] 20 | ) 21 | -------------------------------------------------------------------------------- /Sources/trash/Utilities.swift: -------------------------------------------------------------------------------- 1 | import Foundation 2 | 3 | extension FileHandle: @retroactive TextOutputStream { 4 | public func write(_ string: String) { 5 | write(string.data(using: .utf8)!) 6 | } 7 | } 8 | 9 | enum CLI { 10 | static var standardInput = FileHandle.standardInput 11 | static var standardOutput = FileHandle.standardOutput 12 | static var standardError = FileHandle.standardError 13 | 14 | static let arguments = Array(CommandLine.arguments.dropFirst(1)) 15 | 16 | /// Execute code and print to `stderr` and exit with code 1 if it throws. 17 | static func tryOrExit(_ throwingFunc: () throws -> Void) { 18 | do { 19 | try throwingFunc() 20 | } catch { 21 | print(error.localizedDescription, to: .standardError) 22 | exit(1) 23 | } 24 | } 25 | 26 | /// Revert back to original user if sudo. 27 | static func revertSudo() { 28 | guard 29 | let sudoUIDstring = ProcessInfo.processInfo.environment["SUDO_UID"], 30 | let sudoUID = uid_t(sudoUIDstring) 31 | else { 32 | return 33 | } 34 | 35 | setuid(sudoUID) 36 | } 37 | } 38 | 39 | enum PrintOutputTarget { 40 | case standardOutput 41 | case standardError 42 | } 43 | 44 | /// Make `print()` accept an array of items. 45 | /// Since Swift doesn't support spreading... 46 | private func print( 47 | _ items: [Any], 48 | separator: String = " ", 49 | terminator: String = "\n", 50 | to output: inout some TextOutputStream 51 | ) { 52 | let item = items.map { "\($0)" }.joined(separator: separator) 53 | Swift.print(item, terminator: terminator, to: &output) 54 | } 55 | 56 | func print( 57 | _ items: Any..., 58 | separator: String = " ", 59 | terminator: String = "\n", 60 | to output: PrintOutputTarget = .standardOutput 61 | ) { 62 | switch output { 63 | case .standardOutput: 64 | print(items, separator: separator, terminator: terminator) 65 | case .standardError: 66 | print(items, separator: separator, terminator: terminator, to: &CLI.standardError) 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /Sources/trash/main.swift: -------------------------------------------------------------------------------- 1 | import Foundation 2 | 3 | let VERSION = "2.0.0" 4 | 5 | func trash(_ urls: [URL]) { 6 | // Ensures the user's trash is used. 7 | CLI.revertSudo() 8 | 9 | for url in urls { 10 | CLI.tryOrExit { 11 | try FileManager.default.trashItem(at: url, resultingItemURL: nil) 12 | } 13 | } 14 | } 15 | 16 | func prompt(question: String) -> Bool { 17 | print(question, terminator: " ") 18 | 19 | guard 20 | let input = readLine(), 21 | !input.isEmpty 22 | else { 23 | return false 24 | } 25 | 26 | return ["y", "yes"].contains(input.lowercased()) 27 | } 28 | 29 | guard let argument = CLI.arguments.first else { 30 | print("Specify one or more paths", to: .standardError) 31 | exit(1) 32 | } 33 | 34 | switch argument { 35 | case "--help", "-h": 36 | print("Usage: trash [--help | -h] [--version | -v] [--interactive | -i] […]") 37 | exit(0) 38 | case "--version", "-v": 39 | print(VERSION) 40 | exit(0) 41 | case "--interactive", "-i": 42 | for url in (CLI.arguments.dropFirst().map { URL(fileURLWithPath: $0) }) { 43 | guard FileManager.default.fileExists(atPath: url.path) else { 44 | print("The file “\(url.relativePath)” doesn't exist.") 45 | continue 46 | } 47 | 48 | guard prompt(question: "Trash “\(url.relativePath)”?") else { 49 | continue 50 | } 51 | 52 | trash([url]) 53 | } 54 | default: 55 | trash(CLI.arguments.map { URL(fileURLWithPath: $0) }) 56 | } 57 | -------------------------------------------------------------------------------- /build: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | swift build --configuration=release --arch arm64 --arch x86_64 && mv .build/apple/Products/Release/trash . 4 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # macos-trash 2 | 3 | > Move files and folders to the trash 4 | 5 | *Requires macOS 10.13 or later.* 6 | 7 | Since macOS 14, there is now a built-in `trash` command. The benefit of the binary here is that it has a `--interactive` flag. 8 | 9 | ## Install 10 | 11 | ###### [Homebrew](https://brew.sh) 12 | 13 | ```sh 14 | brew install macos-trash 15 | ``` 16 | 17 | ###### [Mint](https://github.com/yonaskolb/Mint) 18 | 19 | ```sh 20 | mint install sindresorhus/macos-trash 21 | ``` 22 | 23 | ###### Manually 24 | 25 | [Download](https://github.com/sindresorhus/macos-trash/releases/latest) the binary and put it in `/usr/local/bin`. 26 | 27 | ## Usage 28 | 29 | ```sh 30 | trash [--help | -h] [--version | -v] [--interactive | -i] […] 31 | ``` 32 | 33 | ## Build 34 | 35 | ```sh 36 | ./build 37 | ``` 38 | 39 | ## Related 40 | 41 | - [trash](https://github.com/sindresorhus/trash) - Cross-platform Node.js version 42 | - [empty-trash](https://github.com/sindresorhus/empty-trash) - Empty the trash 43 | - [macos-wallpaper](https://github.com/sindresorhus/macos-wallpaper) - Manage the desktop wallpaper 44 | - [do-not-disturb](https://github.com/sindresorhus/do-not-disturb) - Control the macOS `Do Not Disturb` feature 45 | - [More…](https://github.com/search?q=user%3Asindresorhus+language%3Aswift+archived%3Afalse&type=repositories) 46 | --------------------------------------------------------------------------------