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

时间:2011-07-18 17:11:20

标签: macos terminal applescript osx-snow-leopard

我正在尝试学习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属性没有被读取,或者这个属性可能没有按照我的想法去做。

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

8 个答案:

答案 0 :(得分:7)

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

  

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

如果您将事件放在$PS1内,以便每次呈现提示时它都会更改。

来源:How do I set Mac OS X 10.6 Terminal tab title programmatically?

答案 1 :(得分:2)

我刚试过这个并且工作正常:

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所以也许Apple改变了它。

答案 2 :(得分:1)

此属性不符合您的想法。根据以下代码,将自定义标题设置为一个选项卡适用于所有窗口中的所有选项卡:

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的终端相比,可以在那里完成更多有用的事情。

答案 3 :(得分:1)

在抓取正确的窗口/标签时,围绕这些命令有一些奇怪的行为,但最终在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打开一个新的终端窗口,从而强制它成为'第一个'(do script也返回创建的选项卡索引,但我无法使用它)。

自定义标题仅适用于该窗口。还插入一行来设置终端选项卡的配置文件。

(参考文献:AppleScript to open named terminal window

其他 奇怪行为的示例:删除do script行导致自定义标题应用于所有窗口,但只有一个窗口接收设置更改!

答案 4 :(得分:1)

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

答案 5 :(得分:1)

我一直在寻找这个并且在评论中提到 @tponthieux ,所有这些脚本都会更改终端窗口标题而不是标签标题。不幸的是,似乎没有选项可以用一个准备好的苹果脚本来更改标签标题,所以我使用密钥完成它,它在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

答案 6 :(得分:0)

OSX Terminal的标题来自一些不同的来源。

1)首选项>窗口:选择终端>首选项>窗口(选项卡)。在这里,您会找到用于设置窗口标题的各种配置。

2)首选项>选项卡:选择终端>首选项>选项卡(选项卡)。在这里,您会找到用于标记标签的各种配置。

3)控制台代码:您可以使用的VT100命令(more info by searching for OSC here

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内,以便它更新您输入的每个命令。

答案 7 :(得分:0)

这是我需要做的:

tell application "Terminal"
  do script "DISABLE_AUTO_TITLE=true" in selected tab of the front window
  do script "echo -n -e \"\\033]0;" & title & "\\007\";" in selected tab of the front window
end tell

ZSH 需要第一行。

但是,它仍然不适用于长时间运行的进程,因为它们的名称继续在选项卡标题中占据主导地位。