在OS X中以编程方式获取正在运行的应用程序捆绑包 [英] Programmatically get running application bundles in OS X

查看:70
本文介绍了在OS X中以编程方式获取正在运行的应用程序捆绑包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取所有正在运行的应用程序捆绑包的列表.用户已启动的GUI应用程序,例如正在显示Dock或活动监视器(在某些进程旁边显示一个图标).我发现我可以将sysctl()KERN_PROC_ALL结合使用,以获取所有正在运行的进程的列表,但这不会告诉我它们来自哪个应用程序捆绑包. Minecraft之类的应用程序只是显示为java,并不是很有用.

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.

我确实发现活动监视器中的进程组名称大致显示了我想知道的内容:
(来源: gdries.nl )

I did find that the process group name in activity monitor shows roughly what I want to know:
(source: gdries.nl)

实现语言并不重要.当前在C and go中工作,但是如果确实需要其他环境,那不是问题.我要做的只是检测用户正在运行的应用程序,因此我可以记录每个应用程序的使用时间. (家长控制功能类似,但将其记录在我无法解析的plist文件中)

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)

推荐答案

我找到了一种使用Swift和Cocoa API的方法.大概,使用普通C也应该可以,但这对于我的应用程序来说已经足够了.

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)
}

appNSApplication对象,并且具有捆绑标识符,这是我想知道的.

app is an NSApplication object, and that has a bundle identifier, which is what I wanted to know.

这篇关于在OS X中以编程方式获取正在运行的应用程序捆绑包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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