window enum | | lookup window by | Search

This macOS code retrieves a list of open windows and their titles, storing them in global variables and using two functions: enumWindows to get the list of windows and getTitle to retrieve the title of each window. The main function iterates over the list of windows, calling getTitle to print each title to the console.

Run example

npm run import -- "enum windows with cocoa"

enum windows with cocoa


@import Cocoa;

static NSArray* windows;
static NSUInteger count;

#if __cplusplus
extern "C" {
#endif

__attribute__((visibility("default")))
int enumWindows()
{
	CGWindowListOption listOptions = kCGWindowListOptionAll;
	listOptions |= kCGWindowListOptionOnScreenOnly;
	listOptions |= kCGWindowListExcludeDesktopElements;

	// Ask the window server for the list of windows.
	windows = (NSArray*)CGWindowListCopyWindowInfo(listOptions, kCGNullWindowID);
	count = [windows count];

	return count;
}

__attribute__((visibility("default")))
const char* getTitle(int i) {
	if(windows == NULL) {
		count	= enumWindows();
	}
	//for (NSUInteger i = 0; i < count; i++) {
	//}
	NSDictionary*   nswindowsdescription = [windows objectAtIndex:i];
	//NSNumber* windowid = (NSNumber*)[nswindowsdescription objectForKey:@"kCGWindowNumber"];
	NSString* windowName = (NSString*)[nswindowsdescription objectForKey:@"kCGWindowOwnerName"];
	NSNumber* windowLayer = (NSNumber*)[nswindowsdescription objectForKey:@"kCGWindowLayer"];
	NSString* res = [NSString stringWithFormat:@"%@: %s", windowLayer, [windowName UTF8String]];
	return (char *)[res UTF8String];
}


int main(int argc, char *argv[])
{
    int count = enumWindows();
		for(int i = 0; i < count; i++) {
			printf("%s\n", getTitle(i));
		}
}

#if __cplusplus
}
#endif

What the code could have been:

#import 

// Global variables to store the list of windows and its count
static NSArray* windows;
static NSUInteger count;

// Function to enumerate all windows
__attribute__((visibility("default")))
NSUInteger enumerateWindows(CGWindowListOption listOptions) {
    // Ask the window server for the list of windows.
    windows = (NSArray*)CGWindowListCopyWindowInfo(listOptions, kCGNullWindowID);
    count = [windows count];
    return count;
}

// Function to get the title of a window
__attribute__((visibility("default")))
const char* getWindowTitle(NSString* windowName) {
    // Create a string with the window title
    NSString* windowTitle = [NSString stringWithFormat:@"%@: %s", [windows objectAtIndex:0][@"kCGWindowLayer"], [windowName UTF8String]];
    return (const char*)[windowTitle UTF8String];
}

// Function to get the title of a window at a given index
__attribute__((visibility("default")))
const char* getWindowTitleAtIndex(NSUInteger index) {
    if (windows == nil) {
        enumerateWindows(kCGWindowListOptionAll);
    }
    // Check if the index is within bounds
    if (index < [windows count]) {
        NSDictionary* windowInfo = [windows objectAtIndex:index];
        NSString* windowName = windowInfo[@"kCGWindowOwnerName"];
        return (const char*)[getWindowTitle(windowName) UTF8String];
    } else {
        return NULL;
    }
}

int main(int argc, char *argv[]) {
    // Get the number of windows
    NSUInteger windowCount = enumerateWindows(kCGWindowListOptionAll);
    
    // Print the title of each window
    for (NSUInteger i = 0; i < windowCount; i++) {
        printf("%s\n", getWindowTitleAtIndex(i));
    }
    
    return 0;
}

Code Breakdown

Import and Global Variables

enumWindows Function

getTitle Function

main Function

C++ Compatibility