区分进程的不同实例或 windows 并找到它们的 PID 而不是它们的所有者 PID 以在 macOS 中发送输入事件?

distinguish between different instances or windows of a process and find their PID instead of their owner PID to send input event in macOS?

如果我打开多个 windows/tabs 浏览器,我想区分它们以便我可以从我的应用程序中单独控制那些 windows,该方法是什么?如果我以编程方式将命令发送到具有 PID id 的进程,它将将该命令发送到最近活动的 window,但我想将命令发送到该进程的所有 window。如果我使用 CGWindowListOption 查询 运行 进程的 PID,我会得到 运行 进程的所有者 PID。 let windowsListInfo = CGWindowListCopyWindowInfo(options, CGWindowID(0))

我需要知道并使用可以同时触发多个 window/process 的东西。是不是同一个进程不同window的PID不同?例如:Chrome 选项卡 1 有一个 pid,选项卡 2 将有另一个 pid。如何查找那些 PID 而不是仅查找所有者 PID?

那么我如何才能找出具有多个 window 且所有者 PID 相同的进程的不同进程 ID 或相似属性?

这会激活 TextEdit 中的每个 window 并发送一个 space 击键。

activate application "TextEdit"

tell application "System Events"
    tell application process "TextEdit"
        repeat with theWindow in windows
            perform action "AXRaise" of theWindow
            keystroke " "
        end repeat
    end tell
end tell

但要注意在不按键的情况下这是如何工作的,以下将“A”附加到所有当前打开的文档,而不会将任何内容带到前台或干扰用户的输入。

tell application "TextEdit"
    repeat with theDocument in documents
        set text of theDocument to (text of theDocument) & "A"
    end repeat
end tell

只要有可能,您都希望这样而不是发送击键。