AP.
AP.

Reputation: 5323

how to list the content of the trash using objective-c (in a mac)

I am tying to get the content of the trash in a Cocoa application and I was wondering if this is a special path or if I have to use dedicated functions.

Thanks for your help,

Regards,

Upvotes: 0

Views: 1476

Answers (3)

Tsuneo Yoshioka
Tsuneo Yoshioka

Reputation: 7874

You can use URLForDirectory to get Trash directory related to the file, home directory or external Volume.

NSURL *trashURL = [[NSFileManager defaultManager] URLForDirectory:NSTrashDirectory inDomain:NSUserDomainMask appropriateForURL:dirURL create:NO error:&error];

You can call this function for all volumes mounted by using " mountedVolumeURLsIncludingResourceValuesForKeys"

Upvotes: 3

utahwithak
utahwithak

Reputation: 6325

"Macintosh HD>Users>your username>.Trash" It is hidden to finder but you can locate it in the terminal or otherwise. From then on it is a normal folder and you can do whatever functions you would like to it. Could be done like such

NSError *error=nil;
NSString *path=[NSHomeDirectory() stringByAppendingPathComponent:@".Trash"];
NSArray *folderList=[[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:&error];

Upvotes: 1

JeremyP
JeremyP

Reputation: 86651

There are several special paths for trash items.

  • Each user has a .Trash directory in their home directly.
  • Each mounted volume has a .Trashes directory in its root with a subdirectory for each user. This is so that trash items on removable drives like USB keys stay on the drive.

The above are implementation details (that have stayed constant since 10.0) so I'm not sure if it is possible to rely on them. An alternative to going to the directories is to use the scripting bridge to Finder. There's an example in the Scripting Bridge programming guide.

Upvotes: 5

Related Questions