Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically get running application bundles in OS X

Tags:

c

macos

go

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)

like image 782
Gerco Dries Avatar asked Jan 15 '15 00:01

Gerco Dries


1 Answers

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.

like image 162
Gerco Dries Avatar answered Oct 23 '22 00:10

Gerco Dries