开发者

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. This is my experimental code so far...

tell application "Terminal"
    activate
    set frontIndex to index 开发者_StackOverflow中文版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.

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, 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?


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.


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.


This is what I needed to do:

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

The first line is needed for ZSH.

However, it still doesn't work well for long-running processes, since their names continue to dominate the tab title.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜