[英]How to get current active window using Swift and Cocoa
I'm currently developing a Safari App Extension, that consists of two parts:我目前正在开发一个 Safari 应用扩展,它由两部分组成:
Further the host application offers a global shortcut, which opens a popover in the status bar.此外,主机应用程序提供了一个全局快捷方式,可在状态栏中打开一个弹出窗口。 However, I want to check which application's window is currently active, because I do not want to open the popover if a Safari window is currently active.
但是,我想检查哪个应用程序的 window 当前处于活动状态,因为如果 Safari window 当前处于活动状态,我不想打开弹出窗口。 Is there any way to find out using Swift which application's window is currently active?
有什么方法可以使用 Swift 找出哪个应用程序的 window 当前处于活动状态?
Thank you for your help.谢谢您的帮助。
So, with the comments from Alexander and Wileke I think I found a solution.因此,根据 Alexander 和 Wileke 的评论,我想我找到了解决方案。
With NSWorkspace.shared.frontmostApplication
you can check, if Safari is currently active.使用
NSWorkspace.shared.frontmostApplication
您可以检查 Safari 当前是否处于活动状态。 But as Wileke noted, that does not mean, that it has an active window.但正如 Wileke 所指出的,这并不意味着它有一个活跃的 window。 Therefore, we use
CGWindowListCopyWindowInfo
to first get all windows and check if at least one of them belongs to Safari by comparing the PIDs.因此,我们使用
CGWindowListCopyWindowInfo
首先获取所有windows,并通过比较PID检查其中是否至少有一个属于Safari。
This way, we can safely say that Safari must have currently an active window that receives key events.这样,我们可以肯定地说 Safari 当前必须有一个活动的 window 接收关键事件。 This must be true as there is now way that Safari is front most without any windows or that Safari as an window but is not front most at the same time.
This must be true as there is now way that Safari is front most without any windows or that Safari as an window but is not front most at the same time.
Well, unless I missed something.好吧,除非我错过了什么。 But for now it works.
但现在它有效。
Here is code I came up with:这是我想出的代码:
func safariIsActive() -> Bool {
// Get the app that currently has the focus.
let frontApp = NSWorkspace.shared.frontmostApplication!
// Check if the front most app is Safari
if frontApp.bundleIdentifier == "com.apple.Safari" {
// If it is Safari, it still does not mean, that is receiving key events
// (i.e., has a window at the front).
// But what we can safely say is, that if Safari is the front most app
// and it has at least one window, it has to be the window that
// crrently receives key events.
let safariPID = frontApp.processIdentifier
// With this procedure, we get all available windows.
let options = CGWindowListOption(arrayLiteral: CGWindowListOption.excludeDesktopElements, CGWindowListOption.optionOnScreenOnly)
let windowListInfo = CGWindowListCopyWindowInfo(options, CGWindowID(0))
let windowInfoList = windowListInfo as NSArray? as? [[String: AnyObject]]
// Now that we have all available windows, we are going to check if at least one of them
// is owned by Safari.
for info in windowInfoList! {
let windowPID = info["kCGWindowOwnerPID"] as! UInt32
if windowPID == safariPID {
return true
}
}
}
return false
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.