From 3252401045783beb792d9bce582fa967881958ec Mon Sep 17 00:00:00 2001 From: yoshikazu Date: Mon, 27 Jul 2026 15:41:22 +0000 Subject: [PATCH] Add trash service --- Sources/Photochk/Services/TrashService.swift | 30 ++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 Sources/Photochk/Services/TrashService.swift diff --git a/Sources/Photochk/Services/TrashService.swift b/Sources/Photochk/Services/TrashService.swift new file mode 100644 index 0000000..0a8c757 --- /dev/null +++ b/Sources/Photochk/Services/TrashService.swift @@ -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 + ) + } +}