2017-08-17 93 views
2

我的场景是打开新窗口,并在其中执行脚本。我在寻找一种方式怎么回来之前已激活的窗口,并没有尝试这些2种方式:

1.执行键盘快捷键以忍者之路在Safari浏览器窗口之间切换

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改变变得可见,但不活动的状态,并且需要额外的点击页面使其真正活跃

我也尝试更改新创建的窗口的,但它导致它最小化并减慢后台脚本执行的速度。

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

回答

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 代码上面也没有任何更多的“沉默”,然而,就最大限度地减少视觉干扰,这是如此主观的说它几乎不应该被问到。事实是,通过手动或编程方式移动应用程序的窗口时,总会出现一定程度的视觉干扰。

我唯一的建议是,可能会将新窗口的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" 

这些只是示例,以显示如何进一步沿着你。

+0

@ user34398945非常感谢你,这两个选项完美解决了描述问题:D – volna