Terminate an application from my Cocoa code
-
22-06-2021 - |
Frage
I have to programmatically terminate (not forcibly) an application from my Cocoa code.
Actually, here's what it looks like:
-(BOOL) terminateAppWithBundle:(NSString*)bundle {
NSArray* array = [NSRunningApplication runningApplicationsWithBundleIdentifier:bundle];
if ([array count] > 0){
NSRunningApplication* app = (NSRunningApplication*)[array objectAtIndex:0];
[array makeObjectsPerformSelector:@selector(terminate)];
float time = 0;
while (!app.isTerminated){
[NSThread sleepForTimeInterval:0.2];
time += 0.2;
if (time >= 15){
return NO;
}
}
}
return YES;
}
It works very well... but only on Snow Leopard and Lion.
On Leopard (which i would like to support) the application crashes on start with this error message:
Exception Type: EXC_BREAKPOINT (SIGTRAP)
Exception Codes: 0x0000000000000002, 0x0000000000000000
Crashed Thread: 0
Dyld Error Message:
Symbol not found: _OBJC_CLASS_$_NSRunningApplication
I guess it's because NSRunningApplication is not part of the 10.5 SDK... how can i do the same without using that class?
Lösung
Solved by myself with AppleScript:
-(BOOL) terminateiTunes {
NSAppleScript *closeiTunes = [[NSAppleScript alloc] initWithSource:@"tell application \"iTunes\" to quit"];
[closeXcode executeAndReturnError:nil];
return YES;
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow