gpt4 book ai didi

macos - 是否可以获取 OS X 屏幕上所有窗口的列表

转载 作者:行者123 更新时间:2023-12-03 17:37:31 26 4
gpt4 key购买 nike

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

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

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

我按照此 post 尝试了 Core Graphics 库

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

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

最佳答案

您可能需要的工具是 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 ""

关于macos - 是否可以获取 OS X 屏幕上所有窗口的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59292819/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com