Change OSX Terminal Color and Font from Command Line

Changing fonts and colors should be easy, also with terminals. Alas, neither iTerm2 nor OSX Terminal seem to have an easy way of doing this: you need to define a new profile or change some presets of the current profile.

Step 1.

Grab .colors.csv (Use Convert To button to convert it to csv) and save it in your home. The file contains all colors known to Emacs (with M-x list-colors-display) with the corresponding representation used by Applescript.

Step 2.

Add the following code to your .bash_profile

# Put .colors.csv in your home directory
# Add the following functions to your .bash_profile
#
# Change colors and fonts of the OSX terminal from the command line:
# 
# $ set_foreground_color lime green
# $ set_font "Oxygen Mono" 12


#
# Colors for Apple Terminal
#
function list_colors {
    cat ${HOME}/.colors.csv
}

function grep_apple_color {
    grep "$*" ${HOME}/.colors.csv
}

function get_apple_color {
    egrep "(^|,)$*(,|\t)" ${HOME}/.colors.csv | cut -f 6
}

function set_foreground_color {
    color=$(get_apple_color $*)
    if [ "$color" != "" ] ; then
        osascript -e "tell application \"Terminal\" to set normal text color of window 1 to ${color}"
        echo "Normal test color set to: $*: $color"
    fi
}    

function set_background_color {
    color=$(get_apple_color $*)
    if [ "$color" != "" ] ; then
        osascript -e "tell application \"Terminal\" to set background color of window 1 to ${color}"
        echo "Background color set to: $*: $color"
    fi
}    

function set_theme {
    set_foreground_color $1
    set_background_color $2
}    

function set_font {
    osascript -e "tell application \"Terminal\" to set the font name of window 1 to \"$1\""
    osascript -e "tell application \"Terminal\" to set the font size of window 1 to $2"
}

Finally.

You are done! You can now change colors and fonts from your terminal using the following commands:

set_foreground_color <color-name>
set_background_color <color-name>
set_theme foreground-color <color-name>
set_font <font-name>

For instance:

set_foreground_color lime green
set_font "Oxygen Mono" 12