How do I get a list of the window titles on the Mac OSX?

前端 未结 2 1292
半阙折子戏
半阙折子戏 2020-12-23 18:02

I want to get the list of window titles of the currently running applications.

On windows I have EnumWndProc and GetWindowText.

On Linux I have XGetWindowPro

2条回答
  •  既然无缘
    2020-12-23 18:36

    The CGSPrivate.h header that's floating around isn't directly compatible with OS X 10.8 in that CGSGetWindowProperty() no longer exists (well, it does, but you can't link to it anymore). So add these two lines to the CGSPrivate.h file -- I went ahead and figured this out myself after many hours searching Google -- to get it to work:

    extern CGSConnection CGSDefaultConnectionForThread(void);
    extern CGError CGSCopyWindowProperty(const CGSConnection cid, NSInteger wid, CFStringRef key, CFStringRef *output);
    

    Adapting outis's code, here's a way of iterating through each window title. I have tested this with clang 4.2 on Mountain Lion:

    CFStringRef titleValue;
    CGSConnection connection = CGSDefaultConnectionForThread();
    NSInteger windowCount, *windows;
    
    NSCountWindows(&windowCount);
    windows = (NSInteger*) malloc(windowCount * sizeof(NSInteger));
    if (windows) {
        NSWindowList(windowCount, windows);
        for (int i = 0; i < windowCount; ++i)
        {
            CGSCopyWindowProperty(connection, windows[i], CFSTR("kCGSWindowTitle"), &titleValue);
    
            if(!titleValue) //Not every window has a title
                continue;
    
            //Do something with titleValue here
        }
        free(windows);
    }
    

    Some other stuff I found out includes the following:

    1. No window title exceeds 127 bytes.
    2. Window titles are encoded with kCFStringEncodingMacRoman

    So, if you want it as a C-string, write something like this:

    char *cTitle[127] = {0};
    CFStringGetCString(titleValue,cTitle,127,kCFStringEncodingMacRoman);
    

    Personally, I'd recommend doing it this way since the Accessibility API is a total pain and requires extra permissions.

    Hope this helps someone! Cheers!

提交回复
热议问题