5

I want to change the background color of my terminal. setterm doesn't work on a mac? I did find a command which changes the prompt to include the ANSI code?

PS1="\h \w$ \e[0;32m"

But it doesn't take effect immediately, and I want to have it be part of a script which will launch other programs.

  • Are you specifically looking for a way to send ANSI color codes to change the background color in the emulated color terminal, that should work in any terminal emulator that emulates a color terminal? Or are you looking for something specific to macOS's built-in "Terminal.app" terminal emulator (but might not work with other third-party terminal emulator apps like iTerm)? Or would you be fine with either one? – Spiff Mar 15 '17 at 0:36
  • @Spiff I don't think it needs to work elsewhere, just in my mac terminal, but I would prefer a solution that is compatible with *nix desktops and servers. – McKay Mar 15 '17 at 0:45
7

You can create an AppleScript script to change the colors of the current Terminal tab. You can use the Script Editor found in Applications/Utilities to create the script. One way to change the colors is to use the color schemes that you could manually select for the Terminal window, but perform the selection in a script. E.g., you could create a script with the following commands using the Script Editor

tell application "Terminal"

   set current settings of window 1 to settings set "novel"

end tell

You could substitute "grass", "ocean", "Red Sands", etc., instead of "novel". Inside the Script Editor click on File and then select Save. Save the script wherever you wish to place it as a .scpt file. Assuming you named the file terminal-colors and placed it your ~/Documents directory, from a Terminal window tab, you can run the script using the osascript command osascript ~/Documents/terminal-colors.scpt

Alternatively, you can set specific background colors of your choosing using values you can find for specific colors at colors.csv. E.g., if you wanted to have a hot pink background, you could use the following commands in your script, instead of the ones above:

tell application "Terminal"

   set background-color of window 1 to {65535, 33667, 49601}

end tell

This is an OSX/MacOS solution that wouldn't be portable to Linux/Unix.

You can see other settings you can change for a Terminal window by opening the Terminal "dictionary" in the Script Editor. Click on File, select Open Dictionary, scroll through the list of dictionaries until you see Terminal.app then click on it to select it and then click on the Choose button. Within the dictionary, click on Terminal Suite then tab to see settings you can change for individual Terminal window tabs.

  • This is working, but it's a little annoying (having to create extra applescripts), and (as you mentioned) isn't portable. Thanks! – McKay Mar 15 '17 at 16:56
  • 2
    @McKay You don't need to create AppleScript files, osascript can run AppleScript from a string using -e. osascript -e "tell application… – grg Mar 17 '17 at 0:32
0

Creating an applescipt and then making an alias to it can make this a quick process once set up.

Step1 - Create applescript, save to e.g. ~/Documents/scripts

vi ~/Documents/scripts/changeBGcolor-1green.scpt tell application "Terminal" set current settings of window 1 to settings set 1 end tell

vi ~/Documents/scripts/changeBGcolor-2grey.scpt tell application "Terminal" set current settings of window 1 to settings set 2 end tell

1 an 2 shown above but etc. for numbers going up.

Step2 create alias and source file.

vi ~/.bash_profile alias cgreen="osascript ~/Documents/scripts/changeBGcolor-1green.scpt" alias cgrey="osascript ~/Documents/scripts/changeBGcolor-2grey.scpt"

source ~/.bash_profile

I couldnt find a list of what the numbers actually were so I tested the first 10 and will describe below.

1 = green 2 = grey 3 = red 4 = black 5 = alternated colors for some reason when command repeated (I found useful) 6 = light black 7 = tan 8 = blue 9 = off white 10 = pale yellow

So for example in the shell afterwards

cgreen would change the background to green

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.