本文介绍了如何在 Mac OSX 上获取窗口标题列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取当前正在运行的应用程序的窗口标题列表.

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

在 Windows 上,我有 EnumWndProc 和 GetWindowText.

On windows I have EnumWndProc and GetWindowText.

在 Linux 上,我有 XGetWindowProperty 和 XFetchName.

On Linux I have XGetWindowProperty and XFetchName.

Native Mac 等价物是什么?

What is the Native Mac equivalent?

推荐答案

一些可能有用的参考资料:

A few potentially useful references:

CGSGetWindowProperty不是官方的记录,但我相信你可以将它与 NSWindowList() 的一个项目一起使用,如下(完全未经测试):

CGSGetWindowProperty is not officially documented, but I believe you can use it with the an item of NSWindowList() as follows (completely untested):

OSErr err;
CGSValue titleValue;
char *title;
CGSConnection connection = _CGSDefaultConnection();
int windowCount, *windows, i;

NSCountWindows(&windowCount);
windows = malloc(windowCount * sizeof(*windows));
if (windows) {
    NSWindowList(windowCount, windows);
    for (i=0; i < windowCount; ++i) {
        err = CGSGetWindowProperty(connection, windows[i],
                    CGSCreateCStringNoCopy("kCGSWindowTitle"),
                    &titleValue);
        title = CGSCStringValue(titleValue);
    }
    free(windows);
}

在 AppleScript 中,这真的很简单:

In AppleScript, it's really easy:

tell application "System Events" to get the title of every window of every process

您可以使用 NSAppleScript 从应用程序内部调用 applescript 或使用 appscript 作为 ObjC-AppleScript 桥梁.使用 Leopard,您可以使用 Scripting Bridge(更多未经测试的代码):

You can call applescript from within an application using NSAppleScript or use appscript as an ObjC-AppleScript bridge. With Leopard, you can use the Scripting Bridge (more untested code):

SystemEventsApplication *systemEvents = [SBApplication applicationWithBundleIdentifier:@"com.apple.systemevents"];
SBElementArray *processes = [systemEvents processes];
for (SystemEventsProcess* process in processes) {
    NSArray *titles = [[process windows] arrayByApplyingSelector:@selector(title)];
}

如果您不关心可读性,您甚至可以在一次长通话中尝试一下.

You could even try it in one long call, if you don't care about readability.

SystemEventsApplication *systemEvents = [SBApplication applicationWithBundleIdentifier:@"com.apple.systemevents"];
NSArray *titles = [[[systemEvents processes]
                     arrayByApplyingSelector:@selector(windows)]
               arrayByApplyingSelector:@selector(arrayByApplyingSelector:)
               withObject:@selector(title)];

编译器会抱怨 @selector(title) 是错误的类型,但它应该可以工作.手动滚动一些委托,您可以将调用转换为 [[[[systemEvents processes] windows] title].

The compiler will complain that @selector(title) is the wrong type, but it should work. Hand roll some delegation and you could turn the call into [[[systemEvents processes] windows] title].

这篇关于如何在 Mac OSX 上获取窗口标题列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-27 20:54