3

I am looking for a way to determine the position and dimension of a window that is not part of my application in macOS using Swift (prefered) or Objective-C to program an overlay/hud. Is it possible to read a list of all processes and start from there or is there something similar to the getWindowHandle() function in the Windows api?

1 Answer 1

3

Take a look at CGWindowListCopyWindowInfo:

import CoreGraphics 

if let windowList = CGWindowListCopyWindowInfo([.optionAll], kCGNullWindowID) as? [[String: AnyObject]] {
    for window in windowList {
        let number = window[kCGWindowNumber as String]!
        let bounds = CGRect(dictionaryRepresentation: window[kCGWindowBounds as String] as! CFDictionary)!
        
        let name = window[kCGWindowName as String] as? String ?? ""
        print("number = \(number), name = \(name), bounds = \(bounds)")
    }
} else {
    print("Can't get window list")
}

The function returns an array of CFDictionary, which the code above bridged into [String: AnyObject] for easy working in Swift. Here are required and optional keys for the dictionaries. The keys are all defined as CFString you must bridge them into String.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.