使用Applescript控制多个浏览器

时间:2019-07-04 19:48:46

标签: browser applescript

有没有办法用Applescript同时控制2个不同的野生动物园浏览器窗口并在它们之间切换?像唯一ID这样的东西可以确保哪个命令用于哪个safari窗口。

类似...

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

:-)

感谢您的帮助。-

2 个答案:

答案 0 :(得分:1)

Windows由其绝对ID标识。您可以使用:

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

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

但是,要在2个Safari窗口之间切换,您必须使用其编号:1是最前面的,2位于后面,3位于2之后,... 您可以告诉系统事件将数字2置于数字1之前。这样做,原来的2变为1,数字1变为2。

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

如果只有2个窗口,则上面的行将在2个Safari窗口之间保持切换,每次将第二个窗口置于最前面。

答案 1 :(得分:1)

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

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

AppleScript会记住该特定窗口,即使您将其移动,隐藏,将其放在后台,将其最小化...在重置变量或关闭窗口之前,您可以确保aWindow会指向该特定窗口窗口。实际上,此选项卡的作用相同。你可以说:

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

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

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

,然后如果您导航到其他网页,则变量aWindow仍将指向同一窗口。整洁吧?

因此,您要做的就是为两个窗口创建几个变量,然后您可以根据需要来回切换:

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://stackoverflow.com"
    set index of secondWindow to 1
    set miniaturized of firstWindow to true
end tell