简体   繁体   English

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

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

I'm trying to learn Applescript as I'd like to, eventually, programmatically set the title of the tab in Terminal to whatever context I'm currently working in. Should be a simple task and I've gotten it almost right I think.我正在尝试学习 Applescript,因为我想最终以编程方式将终端中的选项卡标题设置为我目前正在使用的任何上下文。应该是一项简单的任务,我认为它几乎是正确的. This is my experimental code so far...到目前为止,这是我的实验代码......

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

The problem is that when I set the tab's title, the title of all other tabs also get set.问题是当我设置选项卡的标题时,所有其他选项卡的标题也会设置。 However , if I right-click and inspect a tab and then set the title manually on that tab, its title is not affected when I run my code and the title I manually typed in remains.但是,如果我右键单击并检查一个选项卡,然后在该选项卡上手动设置标题,则在我运行代码时它的标题不会受到影响,并且我手动输入的标题仍然存在。 It's as though the title displays custom title property is not being read or perhaps this property does not do what I think it does.就好像title displays custom title属性没有被读取,或者这个属性没有做我认为的那样。

How can I set the title of exactly one tab to a custom value?如何将一个选项卡的标题设置为自定义值?

I you're executing the script from the Terminal itself you can use a simple echo , eg:我您正在从终端本身执行脚本,您可以使用简单的echo ,例如:

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

It event works if you put it inside $PS1 so that it changes each time the prompt is rendered.如果您将它放在$PS1中,它会起作用,以便每次呈现提示时它都会更改。

source: How do I set Mac OS X 10.6 Terminal tab title programmatically?来源: 如何以编程方式设置 Mac OS X 10.6 终端选项卡标题?

I just tried this and it worked fine:我刚试过这个,效果很好:

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

I admit I wasn't using 10.6 so maybe Apple changed it.我承认我没有使用 10.6,所以也许苹果改变了它。

I was looking for this for a while and as @tponthieux mentioned in his comment, all these scripts change the terminal window title not tab title.我一直在寻找这个,正如@tponthieux在他的评论中提到的那样,所有这些脚本都会改变终端 window 标题而不是标签标题。 Unfortunately, it seems there is no option to change the tab title with a ready apple script, so I did it with using keys and it works without a problem on OSX El Capitan.不幸的是,似乎没有选项可以使用现成的苹果脚本更改选项卡标题,所以我使用键完成了它,它在 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

This property does not do what you think it does.此属性不会像您认为的那样做。 Setting the custom title to one tab applies to all tabs in all windows, per this code:根据以下代码,将自定义标题设置为一个选项卡适用于所有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"

I wonder if it has to do with the title relating to the environment—ie, bash , csh , zsh , ksh —and not to individual tabs.我想知道它是否与与环境相关的标题有关——即bashcshzshksh ——而不是与单个选项卡有关。 Even if I quit Terminal and come back in, "bar" still shows everywhere.即使我退出终端并重新进入,“栏”仍然到处显示。 I'll freely admit that I don't know enough about how the CL interface works to know for sure.我会坦率地承认,我对 CL 接口的工作原理知之甚少,无法确定。

At the same time, if you are learning Applescript, I would suggest learning it on something a little less wonky, like the Finder or something.同时,如果您正在学习 Applescript,我建议您在一些不太可靠的东西上学习它,比如 Finder 或其他东西。 There are loads more useful things that can be done there than in Terminal with Applescript.与使用 Applescript 的终端相比,可以在那里完成更多有用的事情。

There's some weird behaviour around these commands when grabbing the correct window/tab, but this ended up working for me in 10.5.8 (Terminal v2.0.2)抓取正确的窗口/选项卡时,这些命令周围有一些奇怪的行为,但这最终在 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

The key here is that do script opens a new terminal window, thereby forcing it to be 'first' ( do script also returns the created tab index, but I couldn't make any use of it).这里的关键是do script打开一个新的终端 window,从而强制它成为“第一”( do script也返回创建的选项卡索引,但我无法使用它)。

The custom title then applies only to that window.然后,自定义标题仅适用于该 window。 Also threw in a line to set the profile for the terminal tab.还投入了一行来设置终端选项卡的配置文件。

(Referenced: AppleScript to open named terminal window ) (参考: AppleScript 打开命名终端 window

Additional Example of weird behaviour: removing the do script line results in the custom title being applied to all windows, but only one window receives the settings set change!奇怪行为的附加示例:删除do script行会导致自定义标题应用于所有windows,但只有一个window 收到设置更改!

As of Mac OS X Lion 10.7, Terminal only sets the custom title property of the target tab/window instead of changing the settings profile (which affects all terminals with that profile).从 Mac OS X Lion 10.7 开始,终端仅设置目标选项卡/窗口的custom title属性,而不是更改设置配置文件(这会影响具有该配置文件的所有终端)。 Prior to 10.7, most—but not all—of the terminal properties applied only to the target terminal;在 10.7 之前,大多数(但不是全部)终端属性仅适用于目标终端; however, a few of them applied to the settings profile used by the terminal.但是,其中一些适用于终端使用的设置配置文件。 Those have been changed in 10.7 to only affect the target terminal.这些已在 10.7 中更改为仅影响目标终端。

The titles for OSX Terminal come from a few different sources. OSX 终端的标题来自几个不同的来源。

1) Preferences > Window: Select Terminal > Preferences > Window (tab). 1) 首选项 > Window:Select 终端 > 首选项 > Window(选项卡)。 Here you will find all kinds of configuration for titling the window.在这里您可以找到用于命名 window 的各种配置。

2) Preferences > Tab: Select Terminal > Preferences > Tab (tab). 2)首选项>选项卡:Select终端>首选项>选项卡(选项卡)。 Here you will find all kinds of configuration for titling the tab.在这里,您将找到用于为选项卡命名的各种配置。

3) Console codes: The VT100 commands you can use ( more info by searching for OSC here ) 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"

NOTE: as Elia Schito said, you can place these console codes inside $PS1 so it updates each command you enter.注意:正如Elia Schito所说,您可以将这些控制台代码放在 $PS1 中,以便它更新您输入的每个命令。

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

相关问题 如何在Mac OS X 10.6上编译Mac OS X 10.5的python扩展? - How do I compile python extensions for Mac OS X 10.5, on Mac OS X 10.6? 如何在Mac OS X 10.6上将clang更新为3.3 - How can I update clang to 3.3 on Mac OS X 10.6 如何设置终端脚本在Mac OS X Snow Leopard启动时运行? - How do I set Terminal scripts to run at start up on Mac OS X Snow Leopard? 如何在 Mac OS X 的终端中反转 cd 命令? - How do I reverse the cd command in the Terminal on Mac OS X? 在没有终端的Mac OS X 10.6上安装matplotlib - Installing matplotlib on Mac OS X 10.6 without terminal 在 10.6 (Snow Leopard) 之后以编程方式设置 Mac OS X 音量 - Setting Mac OS X volume programmatically after 10.6 (Snow Leopard) 如何从OS X 10.6的终端中检测屏幕是否被锁定或屏幕保护程序处于活动状态? - How can I detect if the screen is locked or screensaver is active from the Terminal in OS X 10.6? 如何在 Mac OS X 平台上设置语言环境? - How do I set locale on Mac OS X platforms? 如何在Mac OS X 10.6下使用脚本自动执行新系统配置? - How can I automate new system provisioning with scripts under Mac OS X 10.6? 如何在Mac OS X 10.6上升级到Autoconf 2.6.2或更高版本? - How can I upgrade to Autoconf 2.6.2 or higher on Mac OS X 10.6?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM