使用 applescript 将焦点设置到应用程序的特定窗口 [英] Set focus to specific window of an application using applescript

查看:27
本文介绍了使用 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 并将它们打印到标准输出的脚本.我有这个:

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

唯一发生的事情是我失去了当前终端的焦点(我在其中输入这些命令),无论我指定 3034 还是 2528 作为 ID.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆