• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Quit iTunes
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Quit iTunes


  • Subject: Re: Quit iTunes
  • From: Gregory Weston <email@hidden>
  • Date: Sat, 24 May 2008 19:45:14 -0400

Mr. Gecko wrote:

That is slow because it has to compile. but if I can't get this apple
event to work than I might do that

It's not Cocoa, but it is efficient...

To find out if an app with a given bundle ID is running:

#include "ProcessIsRunningWithBundleID.h"
#include <IOKit/IOCFBundle.h>

int ProcessIsRunningWithBundleID(CFStringRef inBundleID, ProcessSerialNumber* outPSN)
{
int theResult = 0;


ProcessSerialNumber thePSN = {0, kNoProcess};
OSErr theError = noErr;
do {
theError = GetNextProcess(&thePSN);
if(theError == noErr)
{
CFDictionaryRef theInfo = NULL;
theInfo = ProcessInformationCopyDictionary(&thePSN,
kProcessDictionaryIncludeAllInformationMask);
if(theInfo)
{
CFStringRef theBundleID = CFDictionaryGetValue(theInfo, IOBundleIdentifierKey);
if(theBundleID)
{
if(CFStringCompare(theBundleID, inBundleID, 0) == kCFCompareEqualTo)
{
theResult = 1;
}
}
CFRelease(theInfo);
}
}
} while((theError != procNotFound) && (theResult == 0));


  if(theResult && outPSN)
  {
    *outPSN = thePSN;
  }

  return theResult;
}

The second argument can be NULL. If you pass the address of a ProcessSerialNumber, you can then use this routine to quit it.

OSErr QuitApplicationByPSN(const ProcessSerialNumber* inPSN)
{
AppleEvent theQuitEvent = {typeNull, NULL};
AEBuildError theBuildError;
OSErr theError = AEBuildAppleEvent(kCoreEventClass, kAEQuitApplication, typeProcessSerialNumber, inPSN,
sizeof(ProcessSerialNumber), kAutoGenerateReturnID, kAnyTransactionID, &theQuitEvent,
&theBuildError,"");
if(theError == noErr)
{
AppleEvent theReply = {};
theError = AESend(&theQuitEvent, &theReply, kAENoReply | kAENeverInteract,
kAENormalPriority, kNoTimeOut, NULL, NULL);
(void)AEDisposeDesc(&theQuitEvent);
}
return theError;
}


_______________________________________________

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


  • Prev by Date: How many times will a sub-classed NSView be instantiated in a simple Cocoa App?
  • Next by Date: Re: check if app opens with a document on startup
  • Previous by thread: Re: Re: Quit iTunes
  • Next by thread: NSWindow CoreAnimation Delegate not being called
  • Index(es):
    • Date
    • Thread