Created
October 4, 2020 22:08
-
-
Save sassman/3cdcb1bb7522d8d82df3e71ebf66ba9f to your computer and use it in GitHub Desktop.
[MacOS] How to retrieve the window list and print all dict values
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Carbon/Carbon.h> | |
// compile with: | |
// clang -framework carbon get-win.c -o get-win | |
int main(int argc, const char *argv[]) { | |
CGWindowListOption options = kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements; | |
CFArrayRef windows = CGWindowListCopyWindowInfo(options, kCGNullWindowID); | |
CFIndex count = CFArrayGetCount(windows); | |
for (int i = 0; i < count; i++) { | |
CFDictionaryRef windowDict = CFArrayGetValueAtIndex(windows, i); | |
CFStringRef key = CFStringCreateWithCString(NULL, "kCGWindowOwnerName", kCFStringEncodingUTF8); | |
const void *value = nil; | |
if (CFDictionaryGetValueIfPresent(windowDict, key, &value) == 1) { | |
const char *c_value = CFStringGetCStringPtr(value, kCFStringEncodingUTF8); | |
if( c_value == NULL ) { | |
// this is the very strange case, where a window owner name c_value is null, but `CFShow(value)` | |
// yields a string, just without the regular quotation marks | |
// like Finder vs "Finder" | |
CFShow(value); | |
CFShowStr(value); // tells: This is an NSString, not CFString | |
} else { | |
printf("kCGWindowOwnerName = %s\n", c_value); | |
printf("-- Details about the string:"); | |
CFShowStr(value); | |
printf("-- End of Details\n\n"); | |
} | |
} | |
CFRelease(key); | |
CFShow(windowDict); | |
} | |
CFRelease(windows); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment