17

Is there a command that can be used to change the color scheme of the Mac OS X Terminal? I like the idea of being able to change colors depending on scripts I run. So far I am just changing the color of my bash prompt with PS1 which is okay but not as noticeable as I'd like.

1
  • What I want to do is change the terminal background color like it would be changed in the same way as Terminal -> Preferences -> Settings except I want to do this with a command. I want to create a script that will look like this: changeBackgroundTheme; ssh; changeBackgroundTheme. This will help to remind me which windows are currently in ssh. Nov 11, 2010 at 17:59

4 Answers 4

18
+50

Depending on what exactly you want to accomplish, here's a few ideas in AppleScript using your Terminal styles. These are more robust than tput, because this gets reset by colored prompts. etc (at least for me).

This sets all tabs running Python (no SSH server available for testing right now) to Homebrew, the others to Ocean:

tell application "Terminal"
    repeat with w from 1 to count windows
        repeat with t from 1 to count tabs of window w
            if processes of tab t of window w contains "Python" then
                set current settings of tab t of window w to (first settings set whose name is "Homebrew")
            else
                set current settings of tab t of window w to (first settings set whose name is "Ocean")
            end if
        end repeat
    end repeat
end tell

save as script and run as osascript Name.scpt anytime you want to re-color your shells (of course you can wrap this as a shell script or something).

If you want to display all long-running processes differently, use the following condition:

if busy of tab t of window w is true then


Or, you can set the style of a single tab, manually selected:

on run argv
    tell application "Terminal" to set current settings of tab (item 1 of argv as number) of front window to first settings set whose name is (item 2 of argv)
end run

Run it like this:

osascript StyleTerm.scpt 3 Homebrew

-> Third tab of frontmost Terminal window gets Homebrew style!

If you want to modify background windows, replace "front window" with a parenthesized expression like just after "tab". If you always want to modify the selected "current tab", use selected tab instead of tab (item 1 of argv as number).


Add the following to your .bash_profile if the first solution is too manual labour for you:

PROMPT_COMMAND='osascript "/path/to/Name.scpt"'

Now it gets executed before every prompt (only problem: not after starting something, i.e. ssh. But this topic isn't about fancy bash tricks anyway. This is just a pointer.)

0
6

Your scripts can use the tput command to set colors in a portable manner. Try the following script and you will see the terminal clear to a dark cyan background with some bright cyan text.

#!/bin/bash
tput setab 6
tput clear
tput setaf 14
echo Hello World

You can see more information about this in man 5 terminfo in the section called "Color Handling".

You can do the same things by echoing the escape sequences that your terminal recognizes directly. It will be faster, but it may not work using another terminal program. Many of them recognize xterm sequences and here is what the script above would look like using them.

#!/bin/bash
printf "\033[48;5;6m"  # or "\033[46m"
printf "\033[H\033[2J" # your system's clear command does something similar
printf "\033[38;5;14m" # or "\033[96m"
echo Hello World

There's more information on xterm control sequences here.

3
  • This seems to have some effect on the color but setaf doesn't seem to do anything at all for me. What I'd really like is something to change the Mac OS terminal theme like waiwai suggest but programmatically and not using the GUI.
    – satur9nine
    Sep 30, 2010 at 17:46
  • Is there a way to change the theme that is being used similar to Terminal -> Preferences -> Settings? Nov 11, 2010 at 18:02
  • @sixtyfootersdude: There's probably a way to do that with osascript, but I'm not familiar with it. Nov 11, 2010 at 18:35
1

You can use applescript to give every new terminal a random theme.

Edit your .bash_profile and add this command

osascript -e "tell application \"Terminal\" to set current settings of front window to some settings set"

If you get the same random theme terminal you can always hit ⌘I and set it manually.

This is more useful if you get a lot of different looking terminal themes. There are many sites for that, if you look around.

2
  • Like it. I made an alias from this command so I can randomly change to a new setting manually. Oct 10, 2017 at 17:21
  • ⌘I is exactly what I was looking for! Thanks.
    – jerclarke
    Jul 8, 2020 at 18:58
0

This AppleScript works for me on OS X 14.3 (and the other answers do not).

tell application "Terminal"
    repeat with i in windows
        try
            set current settings of tabs in i to settings set "Solarized Dark"
        end try
    end repeat
end tell

The Terminal application seems to have changed how it exposes its tabs and windows in OS X 14.3 so you'll reliably get errors when iterating through the tabs of some of its windows. But these don't seem to be real windows so you can just ignore them (which I do with the try ... end try block).

I tend to inline small scripts like this in my shell scripts like so:

osascript -e "tell application \"Terminal\"
    repeat with i in windows
        try
            set current settings of tabs in i to settings set \"Solarized Dark\"
        end try
    end repeat
end tell"

I personally use this to change between light and dark themes in my terminal automatically (I just need to open a new terminal tab or run my color_mode function). You can see my full code for doing this in my dotfiles repo.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .