duanqinqiao4844 2015-01-15 00:02
浏览 91
已采纳

在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: Activity Monitor Showing Minecraft Launcher Running
(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)

  • 写回答

1条回答 默认 最新

  • doujing1858 2015-01-15 01:36
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)