4

My app puts some files in the trash, but also implements undo to pull them back out again. I need to know if the user empties the trash so I can empty my undo stack.

Do I need to monitor file system events to achieve this, or is there an easier way?

Steve Weller
  • 4,599
  • 4
  • 23
  • 30

3 Answers3

2

You'll probably have to end up watching ~/.Trash, but there is a notification posted when the trash is emptied. Its name is "com.apple.carbon.core.DirectoryNotification" (which is also posted at other times), but it doesn't seem to have anything in the userInfo. However, it does have a string as the object, so that might contain some relevant information. My quick tests seem to indicate that it doesn't. (It looks like a string that's something like "FNObject 555279-101")

Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
  • Thanks. I found an example of using this at https://github.com/erica/GrabUpper/blob/master/grabupper.m. Once I know that a change is possible I can check the trash for empty and respond appropriately. – Steve Weller Mar 21 '11 at 02:56
  • Note also that the trash is not only at `~/.Trash`; a file on an external drive is moved to `/path/to/mountpoint/Trashes/uid/`. – Yuji Jun 02 '11 at 04:34
1

You can use UKKQueue to watch file change (empty thrash) notifications:
http://www.cocoadev.com/index.pl?UKKQueue

Raviprakash
  • 2,410
  • 6
  • 34
  • 56
0

Another option is to use FNSubscribe or FNSubscribeByPath. The notification won't tell you exactly what happened, just that something happened to the directory you subscribed to.

JWWalker
  • 22,385
  • 6
  • 55
  • 76