Home > Software engineering >  MacOS application launch event?
MacOS application launch event?

Time:10-08

I want to collect statistics for applications runned in OS.

So I need to get any events of app start and exit

Is there exist some?

Google say nothing =(

CodePudding user response:

You can listen to events (such as NSWorkspaceDidLaunchApplicationNotification) via NotificationCenter. See the following documentation:

https://developer.apple.com/documentation/appkit/nsworkspacedidlaunchapplicationnotification

In particular, look at the See also -> Responding to Environment Notifications section, which lists many relevant notifications to this task.

CodePudding user response:

class StatsCollector {
    var observer: NSKeyValueObservation
    
    init() {
        self.observer = NSWorkspace.shared
            .observe(\.runningApplications, options: [.initial]) { (model, change) in
                //some code
            }
    }
}
  • Related