In my program I am going to check if a mouse button has been pressed. I can accomplish this by using NSEvent.
With CGWindowListOption I want to get the list of all my open desktop windows and see if Safari is open, however I only want to go further in my code if Safari is actually in the foreground and enabled. How can I accomplish this? As far as I have seen, CGWindowList doesn't give me information if the applications are in the foreground and enabled.
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application
NSEvent.addGlobalMonitorForEvents(matching: .otherMouseDown) { event in
print("klick " + String(event.buttonNumber))
let options = CGWindowListOption(arrayLiteral: CGWindowListOption.excludeDesktopElements, CGWindowListOption.optionOnScreenOnly)
if let windowListInfo = CGWindowListCopyWindowInfo(options, kCGNullWindowID) as? Array<Dictionary<String, Any>> {
for singleWindow in windowListInfo {
if singleWindow["kCGWindowOwnerName"] as! String == "Safari" {
print("Safari ist geöffnet!")
}
}
}
}
}
During research I stumbled onto NSRunningApplication and NSWorkspace but I'm not sure if this is what I'm looking for.