Stephen J. Butler ([email protected]) on 2009-01-10 11:18 AM said:

>> Is there a way to quit a background app, other than having NSTask send a
>> unix 'kill' ?
>
>Yeah: you can call the function kill(). man 2 kill

That's not a 'nice way', as requested in the subject. :)

The nice way is to send a quit AppleEvent, which you can do like so:

static OSStatus SendQuitAppleEventToProcess (ProcessSerialNumber psn)
{
        OSStatus                err = paramErr;
        
        NSAppleEventDescriptor* applicationDesc = [NSAppleEventDescriptor
                descriptorWithDescriptorType:typeProcessSerialNumber
                bytes:&psn
                length:sizeof(psn)];
        if (applicationDesc) {
                NSAppleEventDescriptor* quitAppAppleEvent = 
[NSAppleEventDescriptor
                        appleEventWithEventClass:kCoreEventClass
                        eventID:kAEQuitApplication
                        targetDescriptor:applicationDesc
                        returnID:kAutoGenerateReturnID
                        transactionID:kAnyTransactionID];
                        
                AppleEvent*     quitApplicationAppleEventPtr = 
(AEDesc*)[quitAppAppleEvent
aeDesc];
                if (quitApplicationAppleEventPtr) {
                        AppleEvent              replyEvent;
                        err = AESendMessage (quitApplicationAppleEventPtr, 
&replyEvent,
kAEWaitReply, kAEDefaultTimeout);
                        if (!err) {
                                AEDisposeDesc (&replyEvent);
                        }
                }
        }
        
        return err;
}

You can usually get a psn event for 'background apps'.  See
GetProcessForPID().  There's also KillProcess() if it does not respond
to the quit event.

Sean


_______________________________________________

Cocoa-dev mailing list ([email protected])

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to