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 (Cocoa-dev@lists.apple.com)

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