繁体   English   中英

如何以编程方式设置 Mac OS X 10.6 终端选项卡标题?

[英]How do I set Mac OS X 10.6 Terminal tab title programmatically?

我正在尝试学习 Applescript,因为我想最终以编程方式将终端中的选项卡标题设置为我目前正在使用的任何上下文。应该是一项简单的任务,我认为它几乎是正确的. 到目前为止,这是我的实验代码......

tell application "Terminal"
    activate
    set frontIndex to index of the first window whose frontmost is true
    tell window frontIndex
        set title displays custom title of selected tab to true
        set custom title of selected tab to "Bazzy"
    end tell
end tell

问题是当我设置选项卡的标题时,所有其他选项卡的标题也会设置。 但是,如果我右键单击并检查一个选项卡,然后在该选项卡上手动设置标题,则在我运行代码时它的标题不会受到影响,并且我手动输入的标题仍然存在。 就好像title displays custom title属性没有被读取,或者这个属性没有做我认为的那样。

如何将一个选项卡的标题设置为自定义值?

我您正在从终端本身执行脚本,您可以使用简单的echo ,例如:

echo -n -e "\033]0;Tech-Recipes rules\007"

如果您将它放在$PS1中,它会起作用,以便每次呈现提示时它都会更改。

来源: 如何以编程方式设置 Mac OS X 10.6 终端选项卡标题?

我刚试过这个,效果很好:

tell application "Terminal"
    set custom title of tab 2 of window 1 to "beta"
    set custom title of tab 1 of window 1 to "alpha"
end tell

我承认我没有使用 10.6,所以也许苹果改变了它。

我一直在寻找这个,正如@tponthieux在他的评论中提到的那样,所有这些脚本都会改变终端 window 标题而不是标签标题。 不幸的是,似乎没有选项可以使用现成的苹果脚本更改选项卡标题,所以我使用键完成了它,它在 OSX El Capitan 上没有问题。

tell application "Terminal"
    activate
tell application "System Events"
    keystroke "i" using {shift down,command down}
    keystroke Tab
    keystroke "yourtitlehere"
    key code 53 
end tell

此属性不会像您认为的那样做。 根据以下代码,将自定义标题设置为一个选项卡适用于所有windows 中的所有选项卡:

tell application "Terminal"
    tell window 1
        set title displays custom title of tab 1 to true
        set custom title of selected tab to "foo"
    end tell
    tell window 2
        set title displays custom title of tab 2 to true
        set custom title of selected tab to "bar"
    end tell
end tell
--> RESULT: All tabs in all windows show "bar"

我想知道它是否与与环境相关的标题有关——即bashcshzshksh ——而不是与单个选项卡有关。 即使我退出终端并重新进入,“栏”仍然到处显示。 我会坦率地承认,我对 CL 接口的工作原理知之甚少,无法确定。

同时,如果您正在学习 Applescript,我建议您在一些不太可靠的东西上学习它,比如 Finder 或其他东西。 与使用 Applescript 的终端相比,可以在那里完成更多有用的事情。

抓取正确的窗口/选项卡时,这些命令周围有一些奇怪的行为,但这最终在 10.5.8(终端 v2.0.2)中为我工作

tell application "Terminal"
    do script
    set currWin to index of first window

    tell window currWin 
        set custom title of first tab to "A Custom Title"
    end tell

    set current settings of window currWin to settings set "Grass"
end tell

这里的关键是do script打开一个新的终端 window,从而强制它成为“第一”( do script也返回创建的选项卡索引,但我无法使用它)。

然后,自定义标题仅适用于该 window。 还投入了一行来设置终端选项卡的配置文件。

(参考: AppleScript 打开命名终端 window

奇怪行为的附加示例:删除do script行会导致自定义标题应用于所有windows,但只有一个window 收到设置更改!

从 Mac OS X Lion 10.7 开始,终端仅设置目标选项卡/窗口的custom title属性,而不是更改设置配置文件(这会影响具有该配置文件的所有终端)。 在 10.7 之前,大多数(但不是全部)终端属性仅适用于目标终端; 但是,其中一些适用于终端使用的设置配置文件。 这些已在 10.7 中更改为仅影响目标终端。

OSX 终端的标题来自几个不同的来源。

1) 首选项 > Window:Select 终端 > 首选项 > Window(选项卡)。 在这里您可以找到用于命名 window 的各种配置。

2)首选项>选项卡:Select终端>首选项>选项卡(选项卡)。 在这里,您将找到用于为选项卡命名的各种配置。

3) 控制台代码:您可以使用的 VT100 命令(通过在此处搜索 OSC 获得更多信息

echo -n -e "\033]0;Set icon name (tab) and window title to this.\007"
echo -n -e "\033]1;Set the icon name (tab) to this\007"
echo -n -e "\033]2;Set window title to this\007"

注意:正如Elia Schito所说,您可以将这些控制台代码放在 $PS1 中,以便它更新您输入的每个命令。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM