Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move files to trash using NSWorkspace doesn't allow restore

Tags:

cocoa

From a cocoa app I need to move files to trash and this is enough simple using NSWorkspaceRecycleOperation but the file on trash can't be restore to its original position.

The restore seems to work only from Finder.

I need a solution using standard API and running on Snow Leopard 10.6.x or above

like image 409
dafi Avatar asked Jan 18 '11 06:01

dafi


2 Answers

In 10.8+ NSFileManager trashItemAtURL:resultingItemURL:error: moves file to trash with the "Put Back" option (it also moves the file synchronously and doesn't emit sounds).

There's no good method for older systems, but you can use new method where it's available and have fallback for older systems:

[[NSFileManager defaultManager] 
          respondsToSelector:@selector(trashItemAtURL:resultingItemURL:error:)]
like image 187
Kornel Avatar answered Oct 21 '22 21:10

Kornel


I just now discovered that if I pass a valid NSURL ** instead of nil as the resultingItemURL: parameter to NSFileManager trashItemAtURL:resultingItemURL:error:, then I can put back the files from trash. Even though I do nothing with the resultingItemURL afterwards.

like image 32
vboxu Avatar answered Oct 21 '22 23:10

vboxu