Search code examples
objective-ccocoanotificationsnsworkspace

How to catch NSWorkspaceDidPerformFileOperationNotification?


I've trying to get callback after file operation performing, but I can't catch NSWorkspaceDidPerformFileOperationNotification posting.

[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(finderDidFileOperation:) name:NSWorkspaceDidPerformFileOperationNotification object:[NSWorkspace sharedWorkspace]];
[[NSWorkspace sharedWorkspace] performFileOperation:fileOp source:source destination:item.fullPath files:objects tag:&tag];

object:nil

also didn't works and

[[NSWorkspace sharedWorkspace] addObserver:self forKeyPath:NSWorkspaceDidPerformFileOperationNotification options:NSKeyValueObservingOptionNew context:nil];

too.

What I've doing wrong?


Solution

  • I think the key here is that the NSWorkspace posts the notification to its notification center, not the global default one. From the docs:

    Before this method returns, it posts an NSWorkspaceDidPerformFileOperationNotification to the NSWorkspace object's notification center.

    Try registering for the notification with that notification center instead, like this:

    [[[NSWorkspace sharedWorkspace] notificationCenter] 
        addObserver:self 
        selector:@selector(finderDidFileOperation:) 
        name:NSWorkspaceDidPerformFileOperationNotification 
        object:[NSWorkspace sharedWorkspace]];