How to get current active window using Swift and Cocoa

adur

I'm currently developing a Safari App Extension, that consists of two parts:

  • A host status-bar only application
  • The Safari extension

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. Is there any way to find out using Swift which application's window is currently active?

Thank you for your help.

adur

So, with the comments from Alexander and Wileke I think I found a solution.

With NSWorkspace.shared.frontmostApplication you can check, if Safari is currently active. But as Wileke noted, that does not mean, that it has an active 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.

This way, we can safely say that Safari must have currently an active window that receives key events. 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
    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

how to using swift delegate with cocoa

Autohotkey get width of current active child window

Get thread ID of current active window

Getting null pointer exception trying to get bounds from current active window with swift on OSX

How to get the current active account in tronlink using tronweb?

Cocoa: How to get broadcast IP address in Swift?

How to get the URL of the current window using Selenium WebDriver in C#?

How to get the title of the current Aquamacs window using applescript

How to get url of current window when using Facebook Unity sdk

Need to use second window using Cocoa Bindings and Core Data in Swift

How can I get the process name of the current active window in windows with winapi?

OS X. How to get TISInputSourceRef (keyboard layout) of current active window of all system in daemon?

How to get the current active tab from a new pop up window created by chrome.windows

How do I move my current active window from one monitor to another using a keyboard shortcut?

Highlight current active window

How do I open another window in macOS in Swift with Cocoa

Swift - Cocoa - Change window color?

VSTO Outlook: Get the current location of the active Outlook window (inspector or explorer)

Powershell - Get current Active Window title and set it as a string

How to get current longitude and latitude using CLLocationManager-Swift

How to get current active instance of jquery datepicker

How to get list of current/active TFS users?

How to get the current active sheet in my script?

How to get current active theme in magento 2.0

How to get the name of the currently active window in Python?

How to get active window that is not part of my application?

How to make current page active using CSS?

Cocoa Swift: how to automate click on html using javascript using in WKWebView

How to monitor active window changes using xcb?