Search code examples
objective-cxcodemacosdaemon

Force quit OS X app


Which is the best way to force quit an os x or daemon process if I know the app/daemon name (the name that appears in activity monitor)?

I am using objective C for coding.


Solution

  • You may use Applescript to do that:

    //tell Application to quit
    NSAppleScript* restartApp = [[NSAppleScript alloc] initWithSource:@"tell application \"ApplicationName\" to quit"];
    [restartApp executeAndReturnError:nil];
    

    If the app is not responding you may try

    // define command
    NSString* appName = @"Finder";
    NSString* killCommand = [@"/usr/bin/killall " stringByAppendingString:appName];
    
    // execute shell command
    NSTask *task = [[NSTask alloc] init];
    [task setLaunchPath:@"/bin/bash"];
    [task setArguments:@[ @"-c", killCommand]];
    [task launch];
    

    which kills the app.

    Good luck