Distracted Attention

Move to Trash

I’ve been working on a “Move to Trash” action. I began with the Low-Level File Management document, which in a one-liner tells you to use NSWorkspace performFileOperation and the NSWorkspaceRecycleOperation.

This seemed like great news and I quickly implemented the method. The test file was located on a network volume, and each time I tried to move it to the trash, it failed. Further reading uncovered this:

If no trash folder exists on the volume containing the file, the operation fails.

I turned back to Finder.app to see how it handled this scenario. It turns out that when you attempt to move a file located on a network volume to the trash, its pops up an NSAlert and warns that it will delete the file immediately.

Seeking to replicate Finder’s approach, I began working on how to check if the source volume had a trash folder. Several Google searches later, I landed on the CocoaDev MoveToTrash entry. I began to feel depressed as I read the tales, trials and tribulations of implementing “trash” support.

I scanned through the complaints and categories, then at the very end I read the following (and I could’ve wept for joy):

If you’re on Leopard then you don’t need to. There’s a new API, FSMoveObjectToTrashSync and FSMoveObjectToTrashAsync, just for moving objects to the trash.

Gotta love Apple sometimes. Read more: File Manager Reference

Tags: , ,

Posted by Nick | August 30, 2008 | Filed under Cocoa