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

时间:2017-08-17 01:41:20

标签: applescript

我遇到的情况是新窗口打开,脚本在其中执行。我正在寻找一种方法如何回到以前的活动窗口并尝试这两种方式:

1。执行键盘快捷键

tell application "System Events" to keystroke "§" using command down

注意: 由于窗口交换导致的轻微闪回,令人不愉快。

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

set lastWindow to id of window 1
...
set index of window id lastWindow to 1

注意: 当lastWindow被更改时,它变为可见但处于非活动状态,并且需要额外点击页面才能使其真正活跃

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

问题即可。那么有没有办法创建新窗口并以最“沉默”的方式交换回最后一个窗口?

1 个答案:

答案 0 :(得分:1)

参考:

  

注意:当lastWindow被更改时,它会变为可见但处于非活动状态,并且需要在页面上进行额外点击才能使其真正有效

以下示例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 代码,它就不会再沉默了#34;静音"然而,就最小化视觉干扰而言,这是非常主观的,几乎不应该被问到。事实上,当人们通过手动或编程方式移动应用程序的窗口时,总会出现某种程度的视觉干扰。

我唯一的建议是,可能将新窗口的bounds设置为前一个窗口的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" 。然后,当新的和当前前窗中发生的任何事情完成并且您将前一个前窗向前移动时,您将看到整个窗口,而不是现在它背后的新窗口。

{{1}}

这些只是展示如何走得更远的例子。