Add trash service
This commit is contained in:
@@ -0,0 +1,30 @@
|
|||||||
|
import AppKit
|
||||||
|
import Foundation
|
||||||
|
|
||||||
|
/// 選択されたファイルをゴミ箱へ送る。削除ではないので Finder から戻せる。
|
||||||
|
enum TrashService {
|
||||||
|
struct Outcome: Sendable {
|
||||||
|
var trashed: [URL] = []
|
||||||
|
var failed: [URL] = []
|
||||||
|
var errorDescription: String?
|
||||||
|
}
|
||||||
|
|
||||||
|
@MainActor
|
||||||
|
static func moveToTrash(_ urls: [URL]) async -> Outcome {
|
||||||
|
guard !urls.isEmpty else { return Outcome() }
|
||||||
|
|
||||||
|
let (moved, error): ([URL: URL], Error?) = await withCheckedContinuation { continuation in
|
||||||
|
NSWorkspace.shared.recycle(urls) { newURLs, error in
|
||||||
|
continuation.resume(returning: (newURLs, error))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let trashed = urls.filter { moved[$0] != nil }
|
||||||
|
let failed = urls.filter { moved[$0] == nil }
|
||||||
|
return Outcome(
|
||||||
|
trashed: trashed,
|
||||||
|
failed: failed,
|
||||||
|
errorDescription: failed.isEmpty ? nil : error?.localizedDescription
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user