在OS X中以编程方式获取正在运行的应用程序捆绑包
I'm trying to get a list of all running Application Bundles. GUI applications that the user has started, like the Dock is showing, or Activity Monitor (it shows an icon next to certain processes). I found that I could use sysctl()
with KERN_PROC_ALL
to get a list of all running processes, but that won't tell me which application bundle they are from. Applications like Minecraft just show up as java
and that's not very useful.
I did find that the process group name in activity monitor shows roughly what I want to know:
(source: gdries.nl)
The implementation language is not important. Currently working in C and go, but if some other environment turns out to be required that's not a problem. All I want to do is detect which applications the user has running so I can log the time that each has been used. (Parental Controls does something similar but logs it in plist files that I can't parse)
我正在尝试获取所有正在运行的应用程序捆绑包的列表。 用户已启动的GUI应用程序,例如正在显示的Dock或活动监视器(在某些进程旁边显示一个图标)。 我发现我可以将 我确实发现活动监视器中的进程组名称大致显示了我想知道的内容 :
实现语言并不重要。 当前在C and go中工作,但是如果确实需要其他一些环境,那不是问题。 我要做的只是检测用户正在运行的应用程序,因此我可以记录每个应用程序的使用时间。 (家长控制做了类似的操作,但将其记录在我无法解析的plist文件中) p>
div> sysctl() code>与
KERN_PROC_ALL code>一起使用,以获取所有正在运行的进程的列表,但这不会告诉我它们来自哪个应用程序捆绑包。 Minecraft之类的应用程序只是显示为
java code>,这不是很有用。 p>
(来源: gdries.nl ) sub> p>
I found a way to do it using Swift and Cocoa APIs. Presumably, this should also be possible using plain C, but this is good enough for my application.
import Foundation
import AppKit
// Get all running applications
let workspace = NSWorkspace.shared
let applications = workspace.runningApplications
for app in applications {
print(app)
}
app
is an NSApplication
object, and that has a bundle identifier, which is what I wanted to know.