Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terminating Another App Running - Cocoa

How can I terminate another app that is running in cooca. Let's say I have iTunes running, and I type in quit in my app, it would quit itunes. "iTunes" is just an example, it could be anything the user wants. I can open any app from my application, but I want to know how to close any app running.

thanks

kevin

like image 968
lab12 Avatar asked Sep 24 '09 02:09

lab12


2 Answers

AppleScript is a pretty high-level way to send a single Quit event. SIGTERM is a pretty brute-force, low-level way.

The correct way to quit another application is to obtain its Process Serial Number (psn) and send it a kAEQuitApplication Apple event with these two lines of code:

result = AEBuildAppleEvent( kCoreEventClass, kAEQuitApplication, typeProcessSerialNumber, &currentProcessPSN,
sizeof(ProcessSerialNumber), kAutoGenerateReturnID, kAnyTransactionID, &tAppleEvent, &tAEBuildError,"");
result = AESend( &tAppleEvent, &tReply, kAEAlwaysInteract+kAENoReply, kAENormalPriority, kNoTimeOut, nil, nil );        

You can do this from C, C++, or Objective-C, and you have to link with CoreServices.framework.

like image 98
cdespinosa Avatar answered Jan 03 '23 15:01

cdespinosa


If you're running on Mac OS X 10.6, Snow Leopard, you can use the new NSRunningApplication terminate method.

like image 45
Jon Hess Avatar answered Jan 03 '23 16:01

Jon Hess