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

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-05 08:53:57
Elia Schito

I you're executing the script from the Terminal itself you can use a simple echo, e.g.:

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.

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

Clark

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.

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:

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—i.e., bash, csh, zsh, ksh—and not to individual tabs. 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.

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. There are loads more useful things that can be done there than in Terminal with Applescript.

brasskazoo

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)

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).

The custom title then applies only to that window. Also threw in a line to set the profile for the terminal tab.

(Referenced: AppleScript to open named terminal 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!

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). Prior to 10.7, most—but not all—of the terminal properties applied only to the target terminal; 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.

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. 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.

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

The titles for OSX Terminal come from a few different sources.

1) Preferences > Window: Select Terminal > Preferences > Window (tab). Here you will find all kinds of configuration for titling the window.

2) Preferences > Tab: Select Terminal > Preferences > Tab (tab). 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)

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!