3

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.

1
  • Let‘s see whether somebody finds an AppleScript solution for this. How do want font colors handled to make sure you can always read the text in Terminal?
    – nohillside
    Commented Jan 16, 2019 at 23:43

2 Answers 2

5

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:

3
  • Holy GEEZE!!! :-). I need to play with this tomorrow and verify everything. Thank you for the awesome answer.
    – pedz
    Commented Jan 17, 2019 at 3:09
  • I called the big script .xyz.scpt (for now) and then did: while : ;do osascript .xyz.scpt ; sleep 2 ; done and watch the colors change every two seconds... Lots of fun. Thank you again!
    – pedz
    Commented Jan 17, 2019 at 23:54
  • A problem I'm seeing is if I open a new termainal window, then quickly change focus to another window, it is the front window that is affected, not the window just opened. Similar issues occur if, for example an IDE (e.g., PyCharm) opens up a terminal session within it's own window, then whatever Terminal window is frontmost has its background colour changed Commented Feb 25, 2020 at 23:03
1

I added slightly to Yoric's spectacular answer because I prefer what I call "pastel" colors. Replacing the first seven lines of his script long script with:

global kColorValueMaximum
set kColorValueMaximum to 65535
set kColorValueRange to 32768
set kColorValueMinimum to kColorValueMaximum - kColorValueRange

-- Choose a random color for the background
set randomRed to (random number) * kColorValueRange + kColorValueMinimum
set randomGreen to (random number) * kColorValueRange + kColorValueMinimum
set randomBlue to (random number) * kColorValueRange + kColorValueMinimum

Making the Range 16384, to me, was too lite. 32768 sometimes gives a slightly dark color so you might want to tweak that to suite your taste.

You must log in to answer this question.

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