UPDATED 2009-01-13 to correctly detect background applications.
Pass in the bundle-identfier of an application, and this will tell you if it’s currently running or not:
- (BOOL) ThereIsARunningApplicationWithBundleID:(NSString*)bundleID; {
ProcessSerialNumber PSN = {kNoProcess};
while(GetNextProcess(&PSN) == noErr) {
CFDictionaryRef info = ProcessInformationCopyDictionary(&PSN,kProcessDictionaryIncludeAllInformationMask);
if(info) {
NSString *theirBundleID = (NSString*)CFDictionaryGetValue(info, kIOBundleIdentifierKey);
BOOL bundleIDMatches = [bundleID isEqualTo:(NSString*)theirBundleID];
CFRelease(info);
if(bundleIDMatches)
return YES;
}
}
return NO;
}
(It’s a good idea to test if an application is running before sending it an AppleEvent, because that will launch it.)
Interestingly, none of the the Process Manager APIs can see into another user’s process-space; but ps
can; at least on OS X 10.5 and 10.4.