Skip to content

Instantly share code, notes, and snippets.

@atduskgreg
Last active January 2, 2021 22:28
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atduskgreg/5815506 to your computer and use it in GitHub Desktop.
Save atduskgreg/5815506 to your computer and use it in GitHub Desktop.
A utility for printing out the name and windowId for every current window. Use in conjunction with 'screencapture -l' to automate screenshots.
// GetWindowList
// A utility for printing out the name and windowId for every current window.
// Use in conjunction with 'screencapture -l' to automate screenshots.
// compile with gcc thusly:
// gcc -framework carbon -framework foundation -framework cocoa -x objective-c -std=c99 GetWindowList.c -o GetWindowList
#include <Carbon/Carbon.h>
#import <objc/Object.h>
#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>
int main(int argc, char **argv) {
CFArrayRef windowArray = CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly, kCGNullWindowID);
NSMutableArray *windowsInMap = [NSMutableArray arrayWithCapacity:64];
NSArray* windows = (NSArray*)CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly, kCGNullWindowID);
NSUInteger count = [windows count];
for (NSUInteger i = 0; i < count; i++)
{
NSDictionary* nswindowsdescription = [windows objectAtIndex:i];
NSNumber* windowid = (NSNumber*)[nswindowsdescription objectForKey:@"kCGWindowNumber"];
NSString* windowName = (NSString*)[nswindowsdescription objectForKey:@"kCGWindowOwnerName"];
if(windowid)
{
printf("%s:%s\n", [windowName UTF8String], [[windowid stringValue] UTF8String]);
}
}
CFRelease(windowArray);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment