0

I am trying to rename opened terminal tabs in OSX, but I can only change the Terminal title with the code below. Is there a way to change the Tab title with command? I am using osascript with python 2.7.

name = """osascript -e 'tell application "Terminal" to set custom title in selected tab of the front window to "script_1"'"""
os.system(name)

Manually: Shell > Edit Title(SHIFT+COMMAND+I) > Tab Title

hzleonardo
  • 463
  • 1
  • 7
  • 16

4 Answers4

2

Simply passing the following sequence to os.system should work:

>>> import os
>>> tab_title = 'echo "\033]0;%s\007"' % "hello world!"
>>> os.system(tab_title)

0
>>> 
sf89
  • 5,088
  • 7
  • 24
  • 27
joel goldstick
  • 4,393
  • 6
  • 30
  • 46
  • It does the same thing my code does. It changes the terminal title not tab title. – hzleonardo Jun 07 '16 at 22:39
  • I'm on lubuntu linux (LXTerminal 0.1.11), it changed tab and the title in the active tab. Osx is using a different Terminal. I'm not expert on this stuff, but I tinkered, and got it to work. I think there might be an issue with setting a variable in python, then expecting the shell to know it is an environment variable. – joel goldstick Jun 07 '16 at 22:53
  • I guess it is Osx problem, not about python, I tried it with Ubuntu and it works. But I think there is no option to change tab title is Osx yet. – hzleonardo Jun 07 '16 at 23:04
  • Find out what your terminal program is, and search around for the magic strings that might control it – joel goldstick Jun 07 '16 at 23:06
  • Would expect that both do the same thing... \033 (33 octal, 27l) is exactly the same as \x1b (hexadecimal, also 27). Both mean "this is an escape code". Followed by command 0, the title and then terminate with char 7. Perhaps some shells do not interpret octal and some don't do hexadecimal? – Blizz Jan 28 '18 at 12:58
1

I found this one very useful, because it doesn't print unnecessary output to stdout unlike my experiments with subprocess or os.system which resulted in unwanted appearance of '-e' in console.

import sys
sys.stdout.write("\x1b]2;%s\x07" % 'TAB name')
0

This should do:

title='Customized title for TAB'
os.system('echo -n -e "\033]0;{}\007"'.format(title))
j4hangir
  • 2,532
  • 1
  • 13
  • 14
0

Here is the solution with using keys to open inspector, change title and close the inspector. Because according to my research there is no option to change tab title on OSX with using a ready apple script.

It works great, so no need to wait for Apple to release this option.

tabinspector = """osascript -e 'tell application "System Events" to keystroke "i" using {shift down,command down}'"""
pressstab = """osascript -e 'tell application "System Events" to keystroke Tab'""" 
title = """osascript -e 'tell application "System Events" to keystroke "yourtitlehere"'"""
pressesc = """osascript -e 'tell application "System Events" to key code 53'"""

os.system(tabinspector)
os.system(pressstab)
os.system(title)
os.system(pressesc)
hzleonardo
  • 463
  • 1
  • 7
  • 16