user3564870
user3564870

Reputation: 395

Programmatically Getting List of all Running Processes on Mac

I'm looking for a way to get all running processes on the Mac that satisfies these requirements:

1.) ALL processes (system and user)

2.) A unique process name for a given vendor's process. For example, an application from two vendors might have the same process name (as seen in Activity Monitor). With the below code, the bundleIdentifier would give an unique name. The activity monitor might show multiple processes with the same process name, but that process name would be unique to that process.

3.) If the process has a window

The code below will do 2 and 3, but not 1 (it only gets the user's processes).

for (NSRunningApplication app in [[NSWorkspace sharedWorkspace] runningApplications]) {
    NSString *uniqueName = app.bundleIdentifier;
    BOOL hasWindow = (app.activationPolicy == NSApplicationActivationPolicyRegular)?YES:NO;
}

Does anyone know of another way to satisfy all 3 requirements?

Upvotes: 1

Views: 1483

Answers (1)

bbum
bbum

Reputation: 162712

Go pull the sources for ps from the Darwin Sources archive (it'll be buried in one of the system tools archives and not on its own) and start there.

That'll get you (1). (2) isn't possible; two processes can have the same name (but every process has to have a unique PID-- use that).

(3) You'll have to go and plunder the window server somehow. There are probably snippets on GitHub that demonstrate how to do that, I'd think.

Upvotes: 1

Related Questions