我正在尝试在运行ssh
时制作终端更改个人资料。为此,我编写了这个脚本(并定义了一个alias
,以便ssh
运行它):
#!/bin/bash
osascript -e "tell application \"Terminal\" to set current settings \
of front window to first settings set whose name is \"AmadanRemote\""
/usr/bin/ssh "$*"
osascript -e "tell application \"Terminal\" to set current settings \
of front window to first settings set whose name is \"AmadanLocal\""
这几乎可以满足我的需求。 (它在同一窗口中绘制连续的标签错误,因为配置文件显然是在窗口范围内,但我不使用标签。)问题是,如果连接关闭,而另一个终端窗口在顶部,AmadanLocal
配置文件将应用于错误的窗口。
因此,问题是:有没有办法通过终端的tty
设备或任何其他功能(而不是变化无常的front window
)明确地识别终端窗口或标签?
如果没有,是否有任何识别功能可以返回第一个osascript
调用,这将明确识别第二个osascript
调用中的相同窗口/选项卡?
(不一定是AppleScript - 如果JavaScript能够实现,JavaScript也可以。)
编辑:如果有人感兴趣,脚本的最终形状是:
#!/bin/bash
tty=`tty`
osascript <<EOF
tell application "Terminal"
set W to the first window whose tty of tab 1 is "$tty"
set T to tab 1 of W
set the current settings of T to the first settings set whose name is "AmadanRemote"
end tell
EOF
/usr/bin/ssh "$*"
osascript <<EOF
tell application "Terminal"
set W to the first window whose tty of tab 1 is "$tty"
set T to tab 1 of W
set the current settings of T to the first settings set whose name is "AmadanLocal"
end tell
EOF
奖励:这实际上每个标签都做对了! &LT; 3
答案 0 :(得分:2)
是的,有。在脚本编辑器中,我可以运行此命令:
tell application "Terminal" to get {properties, properties of tab 1} of window 1
这将获得前窗和活动选项卡的所有属性。这是输出:
{
{
selected tab:tab 1 of window id 15491 of application "Terminal",
closeable:true,
size:{550,777},
zoomed:false,
frame:{730,0,1280,777},
index:1,
visible:true,
position:{730,23},
class:window,
origin:{730,0},
name:"~ — fish /Users/CK — ttys001",
miniaturizable:true,
frontmost:false,
id:15491, <---------------------------------------①
miniaturized:false,
resizable:true,
bounds:{730,23,1280,800},
zoomable:true
},
{
font:"mononoki-Regular",
title displays device name:true,
cursor color:{64587,609,65480},
current settings:current settings of tab 1 of window id 15491 of application "Terminal",
title displays shell path:false,
tty:"/dev/ttys001", <-----------------------------②
normal text color:{52428,52428,52427},
title displays window size:false,
title displays custom title:true,
contents:"Last login: Mon Jun 18 04:54:37 on ttys001\nCK@CK-mac ~> ",
row:39,
process:{
"login",
"-fish"
},
clean commands:{
"screen",
"tmux"
},
font antialiasing:true,
background color:{16383,16383,16383},
title:"fish /Users/CK",
class:tab,
title displays file name:false,
history:"Last login: Mon Jun 18 04:54:37 on ttys001\nCK@CK-mac ~> ",
selected:true,
size:16,
bold text color:{65535,65535,65535},
busy status:false,
column:60
}
}
该输出的前半部分是窗口的属性,输出的后半部分是同一窗口中选项卡的属性。
我用编号箭头①和②标记了两个注释属性。
属性①属于窗口,它是窗口的唯一id
。这将在窗口的整个生命周期内固定不变。因此,在脚本的某个位置,将id of the front window
存储在变量中,并通过以下内容引用窗口:
tell application "Terminal"
set wID to the id of the front window
set visible of window id wID to false
end tell
事实上,如果您选择将实际的id
对象存储在变量而不是window
中,则根本不需要引用id
:
tell application "Terminal"
set W to the front window
set visible of W to false
end tell
由于AppleScript通过窗口的window
值引用id
对象,因此以同样的方式执行它同样可靠且不变。
从窗口的可用属性列表中,我看不到current settings
属性。但是,该选项卡有current settings
属性。我知道你不使用标签,但在AppleScript中,终端的每个标签实际上只属于一个窗口,这是一种非常直观的标签窗口关系。
因此,您可以预期,包含三个选项卡的窗口将具有对单个window
对象和三个tab
对象的AppleScript引用。通过索引引用它们(可以更改,因此不是在代码中引用它们的好方法),您可能会期望tab 1 of window 1
,tab 2 of window 1
和tab 3 of window 1
。相反,您获得的是tab 1 of window 1
,tab 1 of window 2
和tab 1 of window 3
。
我不知道为什么它会像那样实现。但是,基本上,tab
对象和window
对象似乎可以引用您在屏幕上看到的相同框架界面,希望将其称为window
,实际上它可能是,出于某些目的,tab
。
current settings
是您可能认为自己想要为window
设置它的时间之一,但实际上您想为tab
设置它。
选项卡有一个属性,正如您所希望的那样,称为tty
,它是一个字符串值,用于标识选项卡正在使用的终端。就我而言,它是"/dev/ttys001"
。但是,正如我刚才解释的那样,每个window
对象实际上只包含一个来自AppleScript奇怪观点的tab
个对象。因此,如果您对包含它的窗口有引用,则引用该选项卡很简单:它始终为tab 1 of window id wID
或tab 1 of W
,但您之前存储了变量。
但属于选项卡的tty
属性可用于引用其包含窗口,这意味着如果您知道自己所在的终端,则始终能够识别并引用正确的{{1在正确的tab
中。而且你这样做:
window