Re: Any nice way to quit a background/helper app?
Re: Any nice way to quit a background/helper app?
- Subject: Re: Any nice way to quit a background/helper app?
- From: has <email@hidden>
- Date: Sat, 10 Jan 2009 21:43:31 +0000
Sean McBride wrote:
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
Only if the process has a Carbon/Cocoa event handling loop, mind. If
it's a unix process, use kill() to send the appropriate signal.
FWIW, if you do need to send a 'quit' Apple event, here's what I use
(it provides more thorough error checking):
OSStatus QuitApplicationProcessWithPID(pid_t pid) {
AppleEvent evt, res;
AEDesc errDesc;
OSStatus err;
// build and send a 'quit' event
err = AEBuildAppleEvent(kCoreEventClass, kAEQuitApplication,
typeKernelProcessID,
&pid, sizeof(pid),
kAutoGenerateReturnID,
kAnyTransactionID,
&evt, NULL, "");
if (err) return err;
err = AESendMessage(&evt, &res, kAEWaitReply, kAEDefaultTimeout);
AEDisposeDesc(&evt);
// note: process may quit without replying
if (err == connectionInvalid) return noErr;
if (err) return err;
// check if reply event contains an error number, e.g.
userCanceledErr
err = AEGetParamDesc(&res, keyErrorNumber, typeSInt32, &errDesc);
if (err == noErr) {
AEGetDescData(&errDesc, &err, sizeof(err));
AEDisposeDesc(&res);
} else if (err == errAEDescNotFound)
err = noErr;
return err;
}
This targets by PID, but can easily be tweaked to use bundle ID,
creator type, process serial number, etc.
HTH
has
--
Control AppleScriptable applications from Python, Ruby and ObjC:
http://appscript.sourceforge.net
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden