【问题标题】:Is it possible to get a list of all windows on the screen on OS X是否可以在 OS X 的屏幕上获取所有窗口的列表
【发布时间】:2020-04-05 03:36:25
【问题描述】:

我想知道是否有办法以编程方式获取屏幕上所有窗口的列表,包括当前应用程序之外的窗口。

我将解释我想要实现的目标。我是最近开始使用 mac 的 linux 用户。我在linux上使用fluxbox桌面管理器。在窗口之间切换时,我可以修改fluxbox直接跳转到我想要的窗口,而不是重复按alt+tab循环到所需的窗口。因为fluxbox是开源的,所以我能够做到这一点,我修改了代码,设置了一个快捷方式来将焦点放在我选择的特定打开的窗口上。

我正在尝试在 mac 上实现相同的目标,我想知道这是否可能。我似乎可以访问我的应用程序拥有的窗口,但我无法访问其他应用程序拥有的窗口

我按照post 尝试了 Core Graphics 库

  • 我首先使用CGWindowListCopyWindowInfo 来获取CGWindowId 的列表
  • 接下来我使用[NSApp windowWithWindowNumber:windowId] 获得NSWindow。但是我得到的NSWindownil,但我的应用程序拥有的窗口除外。

这是 mac 的设计方式,我们无法访问我们自己的应用程序之外的窗口吗?或者有没有办法解决这个问题?如果 Mac 旨在防止应用程序控制其他应用程序的窗口,我想知道 GUI 自动化工具如何在 Mac 上工作。

【问题讨论】:

  • 您无法从其他进程的窗口获取NSWindow 句柄。 NSApp 指的是您自己的应用程序。从您链接的帖子中 - 我会遵循使用 Accessibility API 的建议。

标签: macos cocoa core-graphics


【解决方案1】:

您可能需要的工具是 AppleScript。作为您如何收集此信息的示例,此脚本收集所有窗口的名称并在对话框中显示它们:

set names to {}
tell application "System Events"
    repeat with theProcess in processes
        if not background only of theProcess then
            tell theProcess
                set processName to name
                set theWindows to windows
                repeat with theWindow in theWindows
                    set end of names to (processName & ":\"" & (name of theWindow) & "\"")
                end repeat
            end tell
        end if
    end repeat
end tell

set AppleScript's text item delimiters to "
"
display dialog names as text
set AppleScript's text item delimiters to ""

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多