简体   繁体   English

无法在Cocoa中找到正在运行的应用程序

[英]Unable to find running application in Cocoa

I am getting the list of running applications in Cocoa with the following code: 我使用以下代码获取在Cocoa中运行应用程序的列表:

for (NSRunningApplication *app in [[NSWorkspace sharedWorkspace] runningApplications]) {
   MNSLog(@"%@",[app localizedName]);
}

However an application I started from a terminal session is not appearing in the list ('Terminal' is well appearing). 但是,我从终端会话开始的应用程序没有出现在列表中(“终端”很好地出现)。 The application was started from the same user which is executing the cocoa code. 应用程序是从执行可可代码的同一用户启动的。

Is my launched application under Terminal ? 我在终端下启动了应用程序吗? And in such a case how can I find its name and arguments ? 在这种情况下,我怎样才能找到它的名字和参数?

Running ps in another terminal session show my process properly. 在另一个终端会话中运行ps会正确显示我的进程。

Use an NSTask to execute the ps Shell command. 使用NSTask执行ps Shell命令。 You can check the ps man page to determine which arguments you want to pass it based on the information you want to get back. 您可以检查ps手册页以根据要返回的信息确定要传递的参数。 Use NSPipe and NSFileHandle to get the results from the task. 使用NSPipeNSFileHandle从任务中获取结果。

If you want to do some filtering you can pipe the ps output through grep before your app picks up the result. 如果你想做一些过滤,你可以在应用程序获取结果之前通过grep管道ps输出。

For your first question, I think NSWorkspace can only see apps that use the window server so you will only see Terminal, not the executables that it is running internally. 对于您的第一个问题,我认为NSWorkspace只能看到使用窗口服务器的应用程序,因此您只能看到终端,而不是内部运行的可执行文件。

You can use sysctl or ps command to get a list of all BSD processes. 您可以使用sysctlps命令获取所有BSD进程的列表。 Have look at unable to detect application running with another user 看看无法检测到与其他用户一起运行的应用程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM