以编程方式获取Mac上所有正在运行的进程的列表 [英] Programmatically Getting List of all Running Processes on Mac

查看:82
本文介绍了以编程方式获取Mac上所有正在运行的进程的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种使Mac上满足这些要求的所有正在运行的进程的方法:

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

1.)所有进程(系统和用户)

1.) ALL processes (system and user)

2.)给定供应商过程的唯一过程名称.例如,两个供应商提供的应用程序可能具有相同的进程名称(在活动监视器"中可以看到).使用以下代码,bundleIdentifier将给出一个唯一的名称.活动监视器可能会显示多个具有相同流程名称的流程,但是该流程名称对于该流程而言是唯一的.

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.) 如果进程有窗口

3.) If the process has a window

下面的代码将执行2和3,但不会执行1(仅获取用户的进程).

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?

推荐答案

从Darwin Sources档案库中提取 ps 的源文件(它将被埋在系统工具档案之一中,独自)并从那里开始.

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.

那会让你(1).(2)不可能;两个进程可以具有相同的名称(但是每个进程都必须具有唯一的PID,请使用该PID.).

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)您必须以某种方式去掠夺窗口服务器.我想,GitHub上可能有一些片段演示了如何做到这一点.

(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.

这篇关于以编程方式获取Mac上所有正在运行的进程的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆