Files
photochk/Sources/Photochk/Services/TrashService.swift
T
2026-07-27 15:41:22 +00:00

31 lines
974 B
Swift

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
)
}
}