Do you know how I can get the CGWindow Id of any focussed window (belonging or not to the current application) ?
Thanks in advance for your help :)
Regards,
One way is to use CGWindowListCopyWindowInfo to get the list of all windows like this:
CFArrayRef windowList = CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements, kCGNullWindowID);
Examine this list to find all the windows at window level 0 (these are the normal windows).
I'm fairly certain that the array returned will be in the order the windows are layered on the screen. If not, you can sort by the "windowOrder" key. Look at the SonOfGrab sample code for more on how to use this API.
Related
Edit: Thanks to guys below I learned that windows doesn't have this last activity info. So I'll make this question more precise. I have a list of HWNDs and I want the one that was more recently active. I read of GetLastActivePopup but it seems to not give me the right one, it seems to always give me the same hwnd i pass to it. Is this because popup is different from hwnd?
Old question was this:
I have a list of HWNDs.
I actually used EnumWindowsProc, EnumWindows, and EnumChildWindows to get a list of all windows. I grouped them by PID. (I got PID with GetWindowThreadProcessId). I would like to sort the windows in each each group by last focus time. How can I get the info of when the window was last focused?
In X11 (Unix/Linux) we have this property called _NET_WM_USER_TIME on windows. Is there something similar like this on Windows HWNDs?
Thanks
I'm using CGWindowListCopyWindowInfo to grab the list of windows on the desktop. I'd like to filter out only the visible windows using values of the Window List keys. The one window I'm having trouble is the OSX menu bar. One solution I've thought of using is looking at the X and Y of the window bounds. No visible windows seem to have both of those equal to 0, but I'm not sure how reliable this method is.
One other way to do this would be to compare the PID number to that of the OSX Window Server, but I'm not sure how to get that. Can anyone point me towards the right API or know of a reliable way to filter out the menu bar?
Edit: I have the same code as kondy below with the following additions since the listOptions themselves aren't good enough:
CGRect windowBounds;
CGRectMakeWithDictionaryRepresentation((CFDictionaryRef) [windowInfo objectForKey:(id)kCGWindowBounds], &windowBounds);
if (!(windowBounds.origin.x == 0 && windowBounds.origin.y == 0))
{
// Work with windows that aren't the Menubar
}
I have found an answer to filter out the "Window Server":
CGWindowListOption listOptions = kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements;
CFArrayRef windowList = CGWindowListCopyWindowInfo(listOptions, kCGNullWindowID);
Using these ORed options, I get a result as same as mac's "Windowed processes" in "Activity Manager"
I hope it will help you!
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?
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.
I have the following code snippet:
NSNumber* windowNumber = [entry objectForKey:(id)kCGWindowNumber];
applicationName = [entry objectForKey:(id)kCGWindowOwnerName];
with this I grab the window number and the name from the window server of all Applications running.
What I want to do finally is to create a reference to whatever Window and manipulate its properties, for example if I have the window number of some instance of Safari, I would like to set it back or maximize it or maybe hide it. It is possible to do this ? some idea about how to start?
BTW I found this way of retrieve the "window number" and "application name" in an Apple Code sample Called Son of grab: http://developer.apple.com/library/mac/#samplecode/SonOfGrab/Introduction/Intro.html
if someone are curious.
Finally I accomplished this based on the apple sample Son of grab and by using the QUARTZ EVENT TAPS. This is a right way to filter events and manipulate other applications because it serves as a section 508 enabling technology.
does anyone know if you can get the coordinates of a window using the .NET framework or via pinvoking?
I would have the processID or mainwindowhandle.
HI, you can use
System.Windows.Forms.Control cr= System.Windows.Forms.Control.FromHandle(System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle);
now u can get location of that control.
In Windows Forms API, form.Top and form.Left should do.
If all you have is the process Id, you can iterate through the process's widows using EnumWindowsProc windows API method. once you get the window's handle which you want, you can query for its size and position.