Set focus to specific window of an application using applescript

我是研究僧i 提交于 2019-12-12 08:02:59

问题


How can I set focus to a specific window of a given application using applescript?

I have several iTerm2 windows running on different displays. I want to set focus to a specified window using applescript.

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

which prints 6 integers: 3034, 2528, -1, -1, -1, -1

Bonus Question: What are the four -1's ?

Then I try:

tell application "System Events"
  activate window 3034
end tell

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:


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

I noticed that the "-1" windows have the property visible:false

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


来源:https://stackoverflow.com/questions/14773974/set-focus-to-specific-window-of-an-application-using-applescript

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!