NSDocument, callback when renaming document (from titlebar) finishes

Is there any public API to determine when a document is finished renaming (from its window...when clicking on the title in the titlebar).


I only need to pick up the rename event when the user edits it via the titlebar for certain reasons. I don't need to worry about another process renaming the document. It seems like it'd be nice to get a callback, either from NSWindowDelegate or something. Did I miss it? I can KVO the window's title, I guess? But I really only want to know when the user renames the document via the popover.

Accepted Answer

>> by overriding moveToURL:completionHandler: but not cleanly because there's no way for me to determine in my override if the move succeeded or failed


Sure there is, if I understand you correctly. In your override, invoke super with your own completion handler that looks at the Error parameter, does what it needs, then invokes the original completion handler.


You can't do this by monitoring the window title, because the displayed title is not necessarily unique. However NSWindowController has methods that are called when the document file name changes, and NSDocument also has displayName method. One of those might be an alternative point to intervene, though they're going to be invoked on renames done for other reasons. For that matter, moveToURL:completionHandler: is going to be invoked on a move between directories that doesn't involve renaming.

Yeah that's what I'm doing. Wondering if there was another, cleaner way.


void (^myCompletionHandler)(NSError*) = ^void(NSError*theError)
    {
        if (theError == nil)  {
            //NSLog(@"moved.");
        }
        else {
          //NSLog(@"Failed to move with error: %@",theError);
        }
        ///Call the original completionHandler, if not nil.
       if (completionHandler != nil){
        completionHandler(theError);
        }
    };
    //Pass in my completion handler.
   [super moveToURL:url completionHandler:myCompletionHandler];


But I think this is the only way. Better than overriding the fileURL setter for sure though.

I don't think this technique is unclean. The ability to capture behavior as well as data is what blocks (closures) are all about. Wrapping the original handler is analagous to overriding a method. The only thing I'd do differently (or would have done, in the days when I still wrote code in Obj-C) would be to put the block inline in the "moveToURL:completionHandler" method invocation. I think it's a little clearer when reading the code later.


Another way to monitor changes would be to register a file coordinator, which is going to be notified after the document file has been moved or renamed. However, file coordination is pretty hard to get right, especially with NSDocument, so I'd stick with the easy way if I had a choice.

Yeah, I agree that an inline block is nicer. I just had a local block snippet ready and it was easier to paste at the time in a forum post.

NSDocument, callback when renaming document (from titlebar) finishes
 
 
Q