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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With