Exiting other applications in cocoa

I need to close other applications in cocoa. I have a userInfo dictionary from a notification that tells me the name of the application. I tried the terminate and forceTerminate methods, but they didn’t work (I think they are only available in the snow leopard.)

+5
source share
3 answers

We are using -[NSWorkspace runningApplications]. This requires 10.6 or higher.

void SendQuitToProcess(NSString* named)
{   

    for ( id app in [[NSWorkspace sharedWorkspace] runningApplications] ) 
    {
        if ( [named isEqualToString:[[app executableURL] lastPathComponent]]) 
        {
            [app terminate];
        }
    }

}

otherwise you will have to use AppleScript. You can do something like this:

void AESendQuitToProcess(const char* named)
{
    char temp[1024];

    sprintf(temp, "osascript -e \"tell application \\\"%s\\\"\" -e \"activate\" -e \"quit\" -e \"end tell\"", named);

    system(temp);
}
+3
source

( API, 3-4 OS X), AppleScript. script Obj-C/Python/Java , ( Obj-C, "In Cocoa" ). , NSAppleScript ( ):

// Grab the appName
NSString *appName = [someDict valueForKey:@"keyForApplicationName"];
// Generate the script
NSString *appleScriptString = 
    [NSString stringWithFormat:@"tell application \"%@\"\nquit\nend tell",
                               appName];
// Execute the script
NSDictionary *errorInfo = nil;
NSAppleScript *run = [[NSAppleScript alloc] initWithSource:theScript];
NSAppleEventDescriptor *theDescriptor = [run executeAndReturnError:&errorInfo];
// Get the result if your script happens to return anything (this example
//  really doesn't return anything)
NSString *theResult = [theDescriptor stringValue];
NSLog(@"%@",theResult);

script, ( appName "Safari" ) :

tell application "Safari"
quit
end tell

SO

- Cocoa

+3

AppleEvent, , , . Cocoa -y .

+2

All Articles