Guig
Guig

Reputation: 10395

Given an AXUIElement, how can I know in which NSScreen it is displayed?

I need to manage the position of an external application's window. Using the Accessibility API I'm able to get a reference to its AXUIElement, and then to its frame. I can also set its position to a desired frame.

I would like to know in which screen the window is displayed on, to help position it correctly with respect to its screen's bounds. How can I do this?

I noticed that relying on comparing the window's frame to that of each screen doesn't work, since the the window can be displayed in a screen that has less overlap with it than another one.

For instance here the window (green) is mostly on the left screen but displayed on the right screen:

enter image description here

let window: AXUIElement

// Computed from reading kAXPositionAttribute, kAXSizeAttribute from the window, and inverting the Y axis
let windowFrame: CGRect 

// Those don't work
let screenWhereWindowIsDisplayed = NSScreen.screens.first(where: { $0.frame.contains(windowFrame) })
let screenWhereWindowIsDisplayed = NSScreen.screens.first(where: { $0.frame.contains(windowFrame.origin) })

Upvotes: 1

Views: 19

Answers (0)

Related Questions