我如何在Mac OSX的窗口标题的列表? [英] How do I get a list of the window titles on the Mac OSX?

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

问题描述

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

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

在窗口我有EnumWndProc和GetWindowText时。

On windows I have EnumWndProc and GetWindowText.

在Linux上我有XGetWindowProperty和XFetchName。

On Linux I have XGetWindowProperty and XFetchName.

什么是原生的Mac相同呢?

What is the Native Mac equivalent?

推荐答案

一些潜在有用的参考资料:

A few potentially useful references:


  • <$c$c>NSWindowList()

  • NSWorkspace -launchedApplications 和<一个href=\"http://developer.apple.com/mac/library/documentation/Cocoa/Reference/ApplicationKit/Classes/NSWorkspace%5FClass/Reference/Reference.html#//apple%5Fref/occ/clm/NSWorkspace/runningApplications\"><$c$c>+runningApplications

  • <$c$c>CGWindowListCreate()和<一个href=\"http://developer.apple.com/mac/library/documentation/Carbon/Reference/CGWindow%5FReference/Reference/Functions.html#//apple%5Fref/c/func/CGWindowListCopyWindowInfo\"><$c$c>CGWindowListCopyWindowInfo() (需要10.5)

  • CGSGetWindowProperty()

  • NSWindowList()
  • NSWorkspace -launchedApplications and +runningApplications
  • CGWindowListCreate() and CGWindowListCopyWindowInfo() (requires 10.5)
  • CGSGetWindowProperty()

CGSGetWindowProperty 是<一个href=\"http://$c$c.google.com/p/undocumented-goodness/source/browse/trunk/CoreGraphics/CGSPrivate.h\">not正式记录,但我相信你可以用的一个项目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

您可以使用应用程序<一中调用的AppleScript href=\"http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSAppleScript%5FClass/Reference/Reference.html\">NSAppleScript或使用 appscript 作为ObjC-AppleScript的桥梁。随着Leopard中,你可以使用Scripting大桥(更多未经测试code):

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(标题)是错误的类型,但它应该工作。手卷一些代表团,你可以把该呼叫转变 [[[systemEvents过程]窗口]标题]

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的窗口标题的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆