hzleonardo
hzleonardo

Reputation: 483

Rename Terminal Tabs Automatically with command

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

Upvotes: 0

Views: 2238

Answers (4)

joel goldstick
joel goldstick

Reputation: 4483

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

Upvotes: 2

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

Upvotes: 1

hzleonardo
hzleonardo

Reputation: 483

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)

Upvotes: 0

j4hangir
j4hangir

Reputation: 3069

This should do:

title='Customized title for TAB'
os.system('echo -n -e "\033]0;{}\007"'.format(title))

Upvotes: 0

Related Questions