使用 Applescript 控制多个浏览器

Control several Browsers with Applescript

有什么方法可以同时使用 Applescript 控制 2 个不同的 safari 浏览器 windows 并在它们之间切换?类似于唯一 ID 的东西,可以确定哪些命令适用于哪些 safari window.

类似...

tell application "Safari" id 1
    activate
    ....
end tell

:-)

感谢您的帮助。-

Windows 由它们的绝对 Id 标识。您可以使用 :

查看 ID
tell application "Safari" to set myWindow to every window

它为您提供了所有打开的 windows Safari 的列表,例如:{window id 4519,window id 4426,window id 4514}。有 3 个打开 windows !

但是要在 2 个 Safari 之间切换 windows,您必须使用它们的编号:1 最前面,2 在后面,3 在 2 后面,... 您可以告诉系统事件在编号 1 之前生成编号 2。这样做,旧的 2 变为 1,而 1 变为 2。

tell application "System Events" to perform action "AXRaise" of window 2 of process "Safari"

如果您只有 2 个 windows,上面的行将在您的 2 个 Safari windows 之间不断切换,每次都使第二个 window 成为最前面的一个。

AppleScript 中的

Windows 是代码对象,因此如果您引用其中一个,例如:

tell application "Safari"
    set aWindow to window 1 -- sets a reference to the current front window
end tell

AppleScript 会记住特定的 window,即使您移动它、隐藏它、将它放在后台、最小化它...直到您重置变量或关闭 window,您可以确定 aWindow 将指向那个特定的 window。事实上,这对于选项卡也是一样的。你可以说:

tell application "Safari"
    set aTab to tab 3 of window 1 -- sets a reference to the third tab of the current front window
end tell

即使您将该选项卡移动到另一个 window,AppleScript 仍应跟踪它。同样,如果你有一个 window 指向 Google 搜索引擎,你可以说:

tell application "Safari"
    set aWindow to window "Google" -- sets a reference to the Google window
end tell

然后如果您导航到不同的网页,变量 aWindow 仍将指向相同的 window。整洁吧?

所以你需要做的就是为你的两个windows创建几个变量,你可以随意来回切换:

tell application "Safari"
    -- assuming you have a 'Google' window and a 'Yahoo' window open
    set firstWindow to window "Google"
    set secondWindow to window "Yahoo"
    set URL of document of firstWindow to "https://whosebug.com"
    set index of secondWindow to 1
    set miniaturized of firstWindow to true
end tell