Is there any Carbon/Cocoa/C API available on Macs that I can use to enumerate processes? I'm looking for something like EnumProcesses
on Windows.
My goal is to check from code whether a process is running (by name).
Thanks!
Is there any Carbon/Cocoa/C API available on Macs that I can use to enumerate processes? I'm looking for something like EnumProcesses
on Windows.
My goal is to check from code whether a process is running (by name).
Thanks!
There are a couple ways you can do this:
-[NSWorkspace launchedApplications]
.NSTask
, read the results, and search yourself (or pipe it through grep or something).GetBSDProcessList
function described here: http://developer.apple.com/mac/library/qa/qa2001/qa1123.html (I've used this successfully in the past)Ah, I just found the Process Manager reference
Looks like GetNextProcess
and GetProcessInfo
help in figuring out what's running. As suggested by Dave, GetBSDProcessList
can be used if you're looking for daemons and not just Carbon/Cocoa processes.
In the overview of the NSRunningApplicationClass, it says:
NSRunningApplication is a class to manipulate and provide information for a single instance of an application. Only user applications are tracked; this does not provide information about every process on the system.
and
To access the list of all running applications, use the runningApplications method in NSWorkspace.
I would suggest taking a look at Workspace Services Programming Topics
I am using this code:
+ (BOOL)isAppRunning:(NSString*)appName {
BOOL ret = NO;
ProcessSerialNumber psn = { kNoProcess, kNoProcess };
while (GetNextProcess(&psn) == noErr) {
CFDictionaryRef cfDict = ProcessInformationCopyDictionary(&psn, kProcessDictionaryIncludeAllInformationMask);
if (cfDict) {
NSString *name = [(NSDictionary *)cfDict objectForKey:(id)kCFBundleNameKey];
if (name) {
if ([appName isEqualToString:name]) {
ret = YES;
}
}
CFRelease(cfDict);
}
}
return ret;
}