Terminal – How to Have a Random Background Color in Terminal.app

colormojaveterminal

In X11 days, I had a script that would randomly pick a pastel background color and then open a fresh xterm. This was easy to do because I could just pass an argument for (as I recall) -background.

Does Terminal.app have such an interface? I would prefer to do this via a bash script but I can cope with AppleScript if needed.

I tend to use tabs within one Terminal window which may complicate things as well.

I'm currently on Mojave.

Best Answer

You can indeed use AppleScript to change the Terminal background color.

For example:

tell application "Terminal"
  set background color of first window to {33717, 0, 21000, 32288}
end tell

You can execute it from the command line with:

osascript -e 'tell application "Terminal"' -e 'set background color of first window to {33717, 0, 21000, 32288}' -e 'end tell'

You can then change more attributes:

tell application "Terminal"
    set background color of first window to {33717, 0, 21000, 32288}
    set cursor color of first window to "white"
    set normal text color of first window to "white"
    set bold text color of first window to "yellow"
end tell

To execute this kind of longer script from the Terminal, you can save it into a separate file ~/.terminal_change_color.scpt then, call it from the terminal:

osascript ~/.terminal_change_color.scpt

You can then add it to your ~/.bash_profile so that it executes for every new Terminal window you open:

echo "osascript ~/.terminal_change_color.scpt" >> ~/.bash_profile

Below is a longer script to choose a random background color. As @nohillside noticed, you also need to adjust the text color to make it readable (white text on dark background, and black text on light backgound). The script handles that.

global kColorValueMaximum
set kColorValueMaximum to 65535

-- Choose a random color for the background
set randomRed to (random number) * kColorValueMaximum
set randomGreen to (random number) * kColorValueMaximum
set randomBlue to (random number) * kColorValueMaximum
set myBackgroundColor to {randomRed, randomGreen, randomBlue}

-- Select appropriate text colors based on that background
set {myTextColor, myBoldColor} to my ContrastingTextColors(myBackgroundColor)

-- Now inflict them on the frontmost window
tell application "Terminal"
    set targetWindow to window 1
    set background color of targetWindow to myBackgroundColor
    set cursor color of targetWindow to myTextColor
    set normal text color of targetWindow to myTextColor
    set bold text color of targetWindow to myBoldColor
end tell

on ContrastingTextColors(myColor)
    set whiteColor to {kColorValueMaximum, kColorValueMaximum, kColorValueMaximum, kColorValueMaximum}
    set lightGreyColor to {40000, 40000, 40000, kColorValueMaximum}
    set blackColor to {0, 0, 0, kColorValueMaximum}
    set darkGreyColor to {20000, 20000, 20000, kColorValueMaximum}

    -- From http://www.wilsonmar.com/1colors.htm
    set myRed to (item 1 of myColor) / kColorValueMaximum
    set myGreen to (item 2 of myColor) / kColorValueMaximum
    set myBlue to (item 3 of myColor) / kColorValueMaximum
    set magicY to (0.3 * myRed) + (0.59 * myGreen) + (0.11 * myBlue)
    if (magicY < 0.5) then
        return {whiteColor, lightGreyColor}
    else
        return {blackColor, darkGreyColor}
    end if
end ContrastingTextColors

Alternatively, you can create like 30 themes into your Terminal Preferences > Profiles, and select one randomly:

tell application "Terminal"
    set themes to {"Basic", "Homebrew", "Man Page", "Novel", "Pro"}
    set theme to some item of themes
    set current settings of window 1 to settings set theme
end tell

Credits to: