I am working on an application that moves windows of third party applications around on the screen.
To get an overview of all currently open windows, I use
CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements, kCGNullWindowID);
This returns an array of dictionaries defining every open window. Here's an exemplary dictionary returned:
{
kCGWindowAlpha = 1;
kCGWindowBounds = {
Height = 442;
Width = 475;
X = 3123;
Y = "-118";
};
kCGWindowIsOnscreen = 1;
kCGWindowLayer = 0;
kCGWindowMemoryUsage = 907184;
kCGWindowName = Untitled;
kCGWindowNumber = 7328;
kCGWindowOwnerName = TextEdit;
kCGWindowOwnerPID = 20706;
kCGWindowSharingState = 1;
kCGWindowStoreType = 2;
kCGWindowWorkspace = 3;
},
The dictionary is full of good information used elsewhere but lacks an accessibility object that could be used to modify the windows' positions. Windows are clearly identified by the Window Number.
I am now using the PID (kCGWindowOwnerPID) to create an accessibility object for the window's application:
AXUIElementRef app = AXUIElementCreateApplication(pid);
Followed by retrieving a list of all windows the application has opened using AXUIElementCopyAttributeValues:
NSArray *result;
AXUIElementCopyAttributeValues(
(AXUIElementRef) app,
kAXWindowsAttribute,
0,
99999,
(CFArrayRef *) &result
);
This works and returns an array of AXUIElements. This is where I am stuck. There seems to be no API call to retrieve the Window Number of an accessibility object. Is there any way to either
a) Find the accessibility object's Window Number (to ultimately iterate over the array and find the right window)
or
b) Otherwise clearly match a window described in the array returned by CGWindowListCopyWindowInfo to the Accessibility Objects returned by AXUIElementCopyAttributeValues?
See Question&Answers more detail:os