Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSRunningApplication 'terminated' not Observable

I'm building a program that launches another program and is then supposed to monitor it, and take action if it terminates. When the application is launched, I can get an instance of NSRunningApplication from NSWorkspace.

Now, the documentation states that NSRunningApplication has the property 'terminated' that is key-value observable. I've tried implementing:

[browserInstance addObserver:self 
                          forKeyPath:@"terminated"
                             options:NSKeyValueObservingOptionNew
                             context:NULL];

And:

- (void)observeValueForKeyPath:(NSString *)keyPath 
                      ofObject:(id)object 
                        change:(NSDictionary *)change
                       context:(void *)context  
{  

        NSLog(@"observeValueForKeyPath");  
        if ([keyPath isEqual:@"terminated"])  
        {  
            NSLog(@"terminated");  
        }  
} 

but I never see the observeValueForKeyPath method get tripped. Does anyone know how to make this work, if it is possible? I haven't been able to find any specific examples anywhere online.

like image 257
Iain Delaney Avatar asked Nov 08 '10 20:11

Iain Delaney


2 Answers

Have you tried the keyPath "isTerminated"?

Notice in the documentation for NSRunningApplication, the property terminated specifies the getter isTerminated, rather than the default getter terminated. (Which makes sense, as a Boolean property "is" or "isn't")

This suggests there may be a bug in obj-c property parsing, where the name of the getter is used for the KVO path.

like image 60
bobDevil Avatar answered Sep 22 '22 00:09

bobDevil


I ended up using:

NSNotificationCenter *center = [[NSWorkspace sharedWorkspace] notificationCenter];

    // Install the notifications.

    [center addObserver:self 
               selector:@selector(appLaunched:) 
                   name:NSWorkspaceDidLaunchApplicationNotification 
                 object:nil];
    [center addObserver:self 
               selector:@selector(appTerminated:) 
                   name:NSWorkspaceDidTerminateApplicationNotification 
                 object:nil];

And then implementing the appLaunched and appTerminated methods.

like image 35
Iain Delaney Avatar answered Sep 24 '22 00:09

Iain Delaney