使用applescript将焦点设置到应用程序的特定窗口 [英] Set focus to specific window of an application using applescript
问题描述
如何使用applescript将焦点设置到给定应用程序的特定窗口?
How can I set focus to a specific window of a given application using applescript?
我有几个在不同显示器上运行的iTerm2窗口.我想使用applescript将焦点设置到指定的窗口.
I have several iTerm2 windows running on different displays. I want to set focus to a specified window using applescript.
我需要两件事,一个脚本收集窗口ID并将其打印到stdout.我有这个:
I need two things, one script that collects the window ID's and prints them to stdout. I've got this:
tell application "iTerm"
set wins to id of every window
end tell
会打印6个整数:3034、2528,-1,-1,-1,-1
which prints 6 integers: 3034, 2528, -1, -1, -1, -1
奖金问题:四个-1是什么?
Bonus Question: What are the four -1's ?
然后我尝试:
tell application "System Events"
activate window 3034
end tell
唯一发生的事情是,我失去了当前终端的焦点(在其中键入这些命令),无论我将ID指定为3034还是2528.
Upon which the only thing happening is that I lose focus of my current terminal (in which I am typing these commands), not matter whether I specify 3034 or 2528 as the ID.
推荐答案
您几乎拥有了它.您可以通过仅查看可见窗口来过滤掉"-1"窗口ID:
You almost have it. You can filter out the "-1" window IDs as by only looking at visible windows:
tell application "iTerm 2"
set wins to id of every window whose visible is true
end tell
我通过查看以下结果得出了答案:
I figured this out by looking at the results of:
tell application "iTerm 2" to properties of every window
我注意到"-1"窗口具有属性visible:false
I noticed that the "-1" windows have the property visible:false
然后,您可以将窗口ID直接告诉iTerm应用程序而不是系统事件:
Then you can tell the window ID directly to the iTerm application instead of system events:
tell application "iTerm 2"
activate window 13195
end tell
这篇关于使用applescript将焦点设置到应用程序的特定窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!