The Oddler
The Oddler

Reputation: 6718

Cocoa CGWindowListCopyWindowInfo & AXUIElementSetAttributeValue

I'm using the following piece of code to get all windows:

CFArrayRef windows = CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements, kCGNullWindowID);

This gives me an array of dictionaries as found here: Front most window using CGWindowListCopyWindowInfo

Then I check their bounds to see if the mouse is in it, and the first I find is is the one my mouse is over.

Then I would like to be able to move it. I know how to use AXUIElementSetAttributeValue to move a window, though then I need a AXUIElementRef, which I can't figure out how to get out of the dictionary.

How can I solve this?

Upvotes: 3

Views: 1341

Answers (1)

Peter Hosey
Peter Hosey

Reputation: 96363

There is no way to go from a window number to an AXUIElementRef.

The closest you can get is to find the owner of the window, then ask it through Accessibility for its windows and look for one with the same title—but an app may have more than one window with the same title, so you need to figure out what you'll do in that case.

The only ways to move another application's windows are by AppleScript or by Accessibility. Using Accessibility is much more reliable than AppleScript (many, if not most, apps have incomplete and/or flaky scripting support). You can get the process ID and window title from the same window dictionary that gives the window number, but that's as specific as you can get. With AppleScript, you could only use the window's index within its application's window list, and hope that the list hasn't changed order in the split-second between you computing the index and trying to use it.

Upvotes: 3

Related Questions