How can my Cocoa app be notified when the user empties the trash?
My app puts some files in the tra开发者_高级运维sh, 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?
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"
)
You can use UKKQueue to watch file change (empty thrash) notifications:
http://www.cocoadev.com/index.pl?UKKQueue
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.
精彩评论