Swift delete file (Move to Bin) with (Put Back) functionality using FileManager.default.trashItem

Viewed 184

Using the following code I am able to delete a file which is moved to trash, but there is no "Put Back" functionality. When I right click the file in trash only delete immediately option is available.

let srcURL = URL(fileURLWithPath:filename!)
var trashedPath: NSURL!

try! FileManager.default.trashItem(at: srcURL, resultingItemURL: &trashedPath)

My program is a Finder Quick Action Service written in Swift 5.4, where I am passing one or more filenames with path of image files like .heic, .png, .tiff, and converting it to jpg. I then want to delete the source file and move them to trash. As the files can be from different directories I want to provide the facility where the user can restore the deleted source file to its proper place.

What should be done to get "Put Back" option?

1 Answers

As far as my research suggest that the "Put Back" functionality is only available thru Finder. I came across Trash - Trash by ali-rantakari on github Now I have to implement a way to run this command in swift, which is as follows.

@discardableResult
func shell(_ args: String...) -> Int32 {
    let task = Process()
    task.launchPath = "/usr/bin/env"
    task.arguments = args
    task.launch()
    task.waitUntilExit()
    return task.terminationStatus
}

shell("trash","-F", "\(filename!)") 
Related