Applescript - 将窗口置于前台 [英] Applescript - Bring window to foreground
问题描述
我有一个同时打开多个窗口的应用程序.我想将特定窗口置于前台(我知道它的标题).
I have an application with several windows opened at the same time. I'd like to bring a specific window to foreground (I know its title).
目前我正在使用组合键来完成这项任务,但我想尝试一些不同的方法,因为我在使用这种方法时遇到了一些问题.
At the moment I'm using a combination of keys to achieve this task but I'd like to try something different since I'm experiencing some problems with this approach.
tell application "System Events"
set frontmost of process "appIT" to true
keystroke "1" using command down
delay 0.2
end tell
推荐答案
如果您的应用程序可编写脚本并允许设置窗口的索引,您可以执行以下操作(基于 如何使用 AppleScript(优雅地)激活 Safari 窗口?)
If your application is scriptable and allows setting the index of a window, you can do the following (based on an answer in How do I make a Safari window active using AppleScript (elegantly)?)
to raiseWindow of theApplicationName for theName
tell the application named theApplicationName
activate
set theWindow to the first item of ¬
(get the windows whose name is theName)
if index of theWindow is not 1 then
set index to 1
set visible to false
set visible to true
end if
end tell
end raiseWindow
可见性的切换对于处理切换应用程序时出现的一些奇怪现象是必要的.如果您不切换可见性,则当您离开和切换回应用程序时,该窗口将不会是第一个.不幸的是,这种切换会将窗口缩小到停靠栏,然后将其恢复,这是一个非常戏剧性的 UI 中断.
The toggling of the visibility is necessary to deal with some weirdness that occurs with switching applications. If you don't toggle the visibility, the window won't be the first when you switch away from and back to the application. Unfortunately, this toggling shrinks the window to the dock then restores it, a very dramatic UI disruption.
这是我发现的另一种处理怪异的方法:
Here's another way I've found to deal with the weirdness:
to raiseWindow2 of theApplicationName for theName
tell the application named theApplicationName
activate
set theWindow to the first item of ¬
(get the windows whose name is theName)
if the index of theWindow is not 1 then
set the index of theWindow to 2
tell application "System Events" to ¬
tell application process theApplicationName to ¬
keystroke "`" using command down
end if
end tell
end raiseWindow2
这篇关于Applescript - 将窗口置于前台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!