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.
npm run import -- "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
#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 Cocoa;.windows as an NSArray* and count as an NSUInteger.enumWindows function is declared with the __attribute__((visibility("default"))) attribute, making it visible to other processes.CGWindowListCopyWindowInfo function to retrieve a list of windows from the window server.listOptions variable is set to retrieve all windows, excluding desktop elements.windows variable.count variable.getTitle function is declared with the __attribute__((visibility("default"))) attribute, making it visible to other processes.i as an argument and returns a const char*.windows variable is null, the enumWindows function is called to retrieve the list of windows.windows array using the objectAtIndex:i method.const char*.main function is the entry point of the program.enumWindows function to retrieve the count of windows.getTitle function is called to retrieve the window's title.printf.extern "C" to ensure compatibility with C++ code.#if __cplusplus and #endif directives are used to conditionally include C++-specific code.