Applescript 以忍者的方式在safari窗口之间交换

Applescript 以忍者的方式在safari窗口之间交换,applescript,Applescript,我的场景是新窗口打开,脚本在其中执行。我正在寻找一种方法,如何返回到上一个活动窗口,并尝试了以下两种方法: 1。执行键盘快捷键 tell application "System Events" to keystroke "§" using command down set lastWindow to id of window 1 ... set index of window id lastWindow to 1 注意:不愉快的经历,因为窗口交换导致了轻微的闪回。 2。提供lastWind

我的场景是新窗口打开,脚本在其中执行。我正在寻找一种方法,如何返回到上一个活动窗口,并尝试了以下两种方法:

1。执行键盘快捷键

tell application "System Events" to keystroke "§" using command down
set lastWindow to id of window 1
...
set index of window id lastWindow to 1
注意:不愉快的经历,因为窗口交换导致了轻微的闪回。

2。提供lastWindow个人ID,然后将其带到前台

tell application "System Events" to keystroke "§" using command down
set lastWindow to id of window 1
...
set index of window id lastWindow to 1
注意:当更改最后一个窗口时,它将变为可见但不活动的状态,并且需要在页面上进行额外的单击以使其真正处于活动状态

我也尝试更改新创建的窗口的
visible false
,但这会使其最小化并减慢后台脚本的执行速度

问题。那么,有没有一种方法可以创建新窗口并以最“安静”的方式切换回上一个窗口?

参考:

注意:当更改最后一个窗口时,它将变为可见但不活动的状态,并且需要在页面上进行额外的单击以使其真正处于活动状态

下面的示例AppleScript代码将前一个最前面的窗口提升到顶部,使其具有完全焦点并处于活动状态。无需额外单击

tell application "Safari"
    activate
    set thePreviousFrontWindowID to id of front window
    make new document with properties {URL:"https://www.google.com"}
    delay 4 -- # The delay command is here as a placeholder for whatever you're doing in the new window.
    set index of window id thePreviousFrontWindowID to 1
end tell

tell application "System Events" to perform action "AXRaise" of front window of application process "Safari"
这是本示例AppleScript代码中的最后一行,您需要让窗口始终处于完全聚焦和活动状态

关于:

问题。那么有没有一种方法可以创建新窗口并以最“安静”的方式切换回上一个窗口

例如,由于windows在运行上面的示例AppleScript代码时不会发出任何声音,因此它不能再“沉默”了。然而,就最小化视觉干扰而言,这太主观了,几乎不应该问它。事实上,当你手动或通过编程在应用程序的窗口中移动时,总会有某种程度的视觉干扰

我唯一的建议是,可能将新窗口的
边界设置为前一个窗口的边界。然后,当在新的和当前的前窗口中发生的任何事情都完成时,您将前一个前窗口向前移动,您将看到整个窗口,而不会看到现在在它后面的新窗口

tell application "Safari"
    activate
    set thePreviousFrontWindowID to id of front window
    set thePreviousFrontWindowBounds to bounds of front window
    make new document with properties {URL:"https://www.google.com"}
    set bounds of front window to thePreviousFrontWindowBounds
    delay 4 -- # The delay command is here as a placeholder for whatever you're doing in the new window.
    set index of window id thePreviousFrontWindowID to 1
end tell

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

这些只是一些例子来说明如何走得更远。

@user34398945非常感谢,这两个选项都完美地解决了所描述的问题:D