简体   繁体   English

如何使用 Swift 和 Cocoa 获取当前活动的 window

[英]How to get current active window using Swift and Cocoa

I'm currently developing a Safari App Extension, that consists of two parts:我目前正在开发一个 Safari 应用扩展,它由两部分组成:

  • A host status-bar only application仅主机状态栏应用程序
  • The Safari extension 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.

相关问题 如何在可可中使用Swift委托 - how to using swift delegate with cocoa 可可/如何在应用程序委托中获取对当前窗口的contentViewController的引用? - Cocoa/ How do I get a reference to current window's contentViewController in app delegate? 使用Swift关闭OSX Cocoa应用程序中的弹出窗口时如何调用自定义函数? - How to call custom function when closing popup window in OSX Cocoa Application using Swift? 如何使用Storyboards / Cocoa在Swift 3.x中引用视图的窗口 - How do you reference the view's window in Swift 3.x using Storyboards/Cocoa 如何获取NSMetadataItem的URL-macOS Cocoa Swift 3 - How to get URL of NSMetadataItem - macOS Cocoa Swift 3 Cocoa:如何在 Swift 中获取广播 IP 地址? - Cocoa: How to get broadcast IP address in Swift? 需要在Swift中使用可可绑定和核心数据使用第二个窗口 - Need to use second window using Cocoa Bindings and Core Data in Swift 能够使用 WebView 中的元素拖动窗口(Cocoa、Swift) - Ability to drag a window using an element in WebView (Cocoa, Swift) Cocoa:如何在Swift的视图控制器中设置窗口标题? - Cocoa: How to set window title from within view controller in Swift? 如何使用 Cocoa 在 Swift 中打开 macOS 中的另一个窗口 - How do I open another window in macOS in Swift with Cocoa
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM