PDA

View Full Version : Observe Opening Window?




ArtOfWarfare
May 8, 2013, 04:54 PM
I'm writing a plugin for Xcode. I'd like to monitor events that occur in the left sidebar of each window. To do that, I want to make an NSOutlineViewDelegate which assigns itself as the delegate of the sidebar but passes all methods onto the existing NSOutlineViewDelegate (so it's kind of like I want a superclass... except instead of having one class inherit another, I'm having an instance inherit another.)

Anyways, to do that I need to be able to find new outline views as they're created. I figured that to do this, I would observe new windows as they're added. I added this code:

//Inside of a method that's called when the application finishes launching... I know it's called because it logs at the start and end of the method.
// I have tried 0, 8, and 15 as options, which correspond to none, all, or just prior (that one was a mistake)
[NSApp addObserver:self forKeyPath:@"windows" options:15 context:nil];

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
NSLog(@"Changes observed.");
}

Unfortunately, it never outputs that changes have been observed, despite the fact I open and close windows.

Suggestions? Any idea why my code doesn't work as is?

Edit: Actually, it just logged "changes observed." One time while running the app... it occurred in the same second that the observer was added according to the timestamps on the NSLog statements. It doesn't seem to observe changes when new windows are open and existing ones are closed, though.

I just changed the log to include the keypath of the changed property, and it is windows.

I'm thinking that observing that change means that the pointer for windows changed, not the contents of the NSArray that is being pointed at...

2X Edit: I wasn't able to get this work, but I found that there's a notification when an NSOutlineView has its selection changed. Since ultimately this is what I wanted to get (and it's a lot simpler than my convoluted plan which I'm not thinking was going to work even if I was able to find windows as they opened) - I'm just going to use it.