The usual way to do this is to switch to C and list the serial numbers of the process on the system (return to files before Mac OS X). NSWorkspace has an API, but they do not always work as you expect.
Note that classic processes (on PowerPC systems) will be listed with this code (with different process serial numbers), although they all have the same process identifier.
void DoWithProcesses(void (^ callback)(pid_t)) { ProcessSerialNumber psn = { 0, kNoProcess }; while (noErr == GetNextProcess(&psn)) { pid_t pid; if (noErr == GetProcessPID(&psn, &pid)) { callback(pid); } } }
You can then call this function and pass in a block that will do what you want with the PID.
Using NSRunningApplication and NSWorkspace :
void DoWithProcesses(void (^ callback)(pid_t)) { NSArray *runningApplications = [[NSWorkspace sharedWorkspace] runningApplications]; for (NSRunningApplication *app in runningApplications) { pid_t pid = [app processIdentifier]; if (pid != ((pid_t)-1)) { callback(pid); } } }
source share