How to list windows, and their information, under Microsoft windows, Linux and macOS

 

A window is just a place, where an application can do its drawing, like drawing a button, text or other stuff.

Table of Contents

For Linux

On Linux or UNIX like systems, there is a tool called xwininfo, which can be used to list all the windows, on a system. Hence you would be able to get, the windows IDs, and names.

Additionally, xwininfo can be used, to get the attributes of a specific window, such as, where it is located , its width , height.

Having gotten a window id, you can also query a window for its properties, using xprop . A window property, is a way to exchange information, between windows.

  1. $ xwininfo -root -tree
  2. # List all windows, starting
  3. # from the root of the screen, in
  4. # a tree manner.
  5. # The window id is displayed,
  6. # so for example, for the
  7. # Calculator window, its id is
  8. # 0x800011.
  9. # In addition to the window id,
  10. # its name is displayed, so for
  11. # the window which has an id of
  12. # 0x800011 ,its name is calculator.
  13. xwininfo: Window id: 0xac (the root window) (has no name)

  14.   Root window id: 0xac (the root window) (has no name)
  15.   Parent window id: 0x0 (none)
  16.      2 children:
  17.      0x800011 "Calculator": ("xcalc" "XCalc")  226x394+59+82  +59+82
  18.         1 child:
  19.         0x800012 (has no name): ()  226x394+0+0  +59+82
  20.            56 children:
  21.            0x80004b (has no name): ()  216x46+4+2  +63+84
  22.               1 child:
  23.               0x80004c (has no name): ()  204x38+6+2  +70+87
  24.                  10 children:
  25.                  0x800056 (has no name): ()  10x15+4+2  +75+90
  26.                  0x800055 (has no name): ()  186x17+18+2  +89+90
  27.                  ...
  28.                  0x80004e (has no name): ()  26x15+146+21  +217+109
  29.                  0x80004d (has no name): ()  26x15+146+21  +217+109
  30.            0x80004a (has no name): ()  40x26+4+62  +63+144
  31.            0x800049 (has no name): ()  40x26+48+62  +107+144
  32.            0x800048 (has no name): ()  40x26+92+62  +151+144
  33.            ...
  34.            0x800015 (has no name): ()  40x26+136+362  +195+444
  35.            0x800014 (has no name): ()  40x26+180+362  +239+444
  36.      0x600000 "xwinclip": ()  500x500+1+1  +1+1



  37. $ xwininfo -name Calculator
  38. # List the attributes, of the
  39. # Calculator window, as in its
  40. # width, height, x and
  41. # y positions ..
  42. xwininfo: Window id: 0x800011 "Calculator"

  43.   Absolute upper-left X:  59
  44.   Absolute upper-left Y:  82
  45.   Relative upper-left X:  59
  46.   Relative upper-left Y:  82
  47.   Width: 226
  48.   Height: 394
  49.   Depth: 24
  50.   Visual: 0x21
  51.   Visual Class: TrueColor
  52.   Border width: 0
  53.   Class: InputOutput
  54.   Colormap: 0x20 (installed)
  55.   Bit Gravity State: NorthWestGravity
  56.   Window Gravity State: NorthWestGravity
  57.   Backing Store State: NotUseful
  58.   Save Under State: no
  59.   Map State: IsViewable
  60.   Override Redirect State: no
  61.   Corners:  +59+82  -1081+82  -1081-292  +59-292
  62.   -geometry 226x394+59+82

  63. $ xprop -name Calculator
  64. # List the properties, of the
  65. # window, named Calculator. A
  66. # property is used to exchange
  67. # information, for example,
  68. # between the window, and the
  69. # window manager.
  70. WM_STATE(WM_STATE):
  71.                 window state: Normal
  72.                 icon window: 0x0
  73. WM_PROTOCOLS(ATOM): protocols  WM_DELETE_WINDOW
  74. WM_CLIENT_LEADER(WINDOW): window id # 0x800011
  75. WM_LOCALE_NAME(STRING) = "en_US.UTF-8"
  76. WM_CLASS(STRING) = "xcalc", "XCalc"
  77. WM_HINTS(WM_HINTS):
  78.                 Client accepts input or input focus: True
  79.                 Initial state is Normal State.
  80.                 bitmap id # to use for icon: 0x636c6163
  81. WM_NORMAL_HINTS(WM_SIZE_HINTS):
  82.                 program specified size: 226 by 394
  83.                 window gravity: NorthWest
  84. WM_CLIENT_MACHINE(STRING) = "IESOUIP-OIDMS1P"
  85. WM_COMMAND(STRING) = { "xcalc" }
  86. WM_ICON_NAME(STRING) = "Calc"
  87. WM_NAME(STRING) = "Calculator"

For windows

For Microsoft windows, there are multiple alternatives, to xwininfo, and xprop, that can be used. The most prominent one, is the Spy++ application.

Spy++, allows to to display a list of all the windows. Additionally, for each listed window, you can get a list of its attributes, such as its bounding rectangle, its window procedure, its extended and window styles, the process and thread ids, which have created the window …

Additionally, Spy++ allows to view the messages, which are delivered to a window, such as WM_CREATE, for when the window is created, or WM_KEYDOWN , for when a key is down.





The Spy++ application, can be downloaded from here . It can also be installed, by installing visual studio.

Additional information, about Spy++, can be found here .

macOS

For Apple macOS, there is no software, similar to Spy++, which can be used to list all the windows, and their attributes, but this can be done using the following code.

  1. #import <Cocoa/Cocoa.h>
  2. #include <stdio.h>

  3. CFComparisonResult
  4.             sortWindow
  5.         (CFDictionaryRef window_1, CFDictionaryRef window_2, CFStringCompareFlags compareOptions ){
  6.         //Sort windows using their window number
  7.     NSInteger window_number_1 = [[window_1 objectForKey:(id ) kCGWindowNumber ] integerValue ];
  8.     NSInteger window_number_2 = [[window_2 objectForKey:(id ) kCGWindowNumber ] integerValue ];
  9.     if (window_number_1 > window_number_2 )
  10.         return kCFCompareGreaterThan;
  11.     else if (window_number_1 < window_number_2 )
  12.         return kCFCompareLessThan;
  13.     return kCFCompareEqualTo; }

  14. int
  15.             main
  16.         (int argc, char* argv[ ] ){

  17.     NSAutoreleasePool* pool = [[NSAutoreleasePool alloc ] init ];
  18.     NSArray* windows =  [(NSArray* ) CGWindowListCopyWindowInfo (kCGWindowListOptionOnScreenOnly, kCGNullWindowID ) autorelease ];
  19.     /* Generate and return information about windows, in current user session,
  20.          kCGWindowListOptionOnScreenOnly will list all the windows,
  21.          which are currently on screen  .*/
  22.     CFArraySortValues (windows,
  23.                 CFRangeMake (0, CFArrayGetCount (windows )),
  24.                 (CFComparatorFunction ) sortWindow,
  25.                 NULL);
  26.     /* Sort the windows */


  27.     NSInteger window_number;
  28.     NSString* window_application_owner_name;
  29.     NSInteger window_application_owner_pid;
  30.     CGRect window_rectangle;

  31.     printf ("Window Number    Application Name    Process ID    X         Y      W      H\n");
  32.     printf ("-----------------------------------------------------------------------------\n");

  33.     for ( NSDictionary* window in windows ){
  34.         window_number = [[window objectForKey:(id ) kCGWindowNumber ] integerValue ];
  35.         window_application_owner_name = [window objectForKey:(id ) kCGWindowOwnerName ];
  36.         window_application_owner_pid = [[window objectForKey:(id ) kCGWindowOwnerPID ] integerValue ];
  37.         CGRectMakeWithDictionaryRepresentation (CFDictionaryGetValue (window, kCGWindowBounds ),
  38.                                                &window_rectangle );
  39.         printf ("%-16d %-24s %-8d %-4.0f %6.0f %6.0f %6.0f\n",
  40.                 window_number,
  41.                 [window_application_owner_name UTF8String ],
  42.                 window_application_owner_pid,
  43.                 window_rectangle .origin .x,
  44.                 window_rectangle .origin .y,
  45.                 window_rectangle .size .width,
  46.                 window_rectangle .size .height); }
  47.     [pool release]; }

  48. /*
  49. $ nano list_windows.m
  50. $ gcc -framework Cocoa list_windows.m
  51. $ ./a.out

  52. Output:
  53. Window Number    Application Name    Process ID    X         Y      W      H
  54. -----------------------------------------------------------------------------
  55. 2                Window Server            58       0         0   1366    768
  56. 6                Window Server            58       0         0   1366     22
  57. 11               Finder                   121      0         0   1366    768
  58. 12               Window Server            58       0        22   1366     14
  59. 13               SystemUIServer           120      1320      0     46     22
  60. 14               SystemUIServer           120      1091      0    229     22
  61. 15               SystemUIServer           120      0         0   1366     22
  62. 17               Dock                     119      0        22   1366    746
  63. 20               Dock                     119      26      718   1314     50
  64. 26               Terminal                 171      306      82    865    548
  65. */