Last active
May 20, 2024 03:28
-
-
Save st3v/5008165 to your computer and use it in GitHub Desktop.
Automatically set background color in iTerm depending on ssh host
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# ssh into a machine and automatically set the background | |
# color of Mac OS X Terminal depending on the hostname. | |
# | |
# Installation: | |
# 1. Save this script to /usr/local/bin/ssh-host-color | |
# 2. chmod 755 /usr/local/bin/ssh-host-color | |
# 3. alias ssh=/usr/local/bin/ssh-host-color | |
# 4. export PRODUCTION_HOST="<hostname_production_server>" | |
# 5. Configure your host colors below. | |
# | |
# Taken from http://talkfast.org/2011/01/10/ssh-host-color | |
# | |
set_term_bgcolor(){ | |
local R=$1 | |
local G=$2 | |
local B=$3 | |
/usr/bin/osascript <<EOF | |
tell application "iTerm" | |
tell the current terminal | |
tell the current session | |
set background color to {$(($R*65535/255)), $(($G*65535/255)), $(($B*65535/255))} | |
end tell | |
end tell | |
end tell | |
EOF | |
} | |
if [[ "$@" =~ "$PRODUCTION_HOST" ]]; then | |
set_term_bgcolor 50 0 0 | |
else | |
set_term_bgcolor 0 40 0 | |
fi | |
ssh $@ | |
set_term_bgcolor 0 0 0 | |
clear |
Doesn't work as of now unfortunately. Gives the error:
44:52: syntax error: Expected end of line but found identifier. (-2741)
It's complaining about the terminal
bit (despite that not being on line 44?).
Probably easier to use their escape codes: https://iterm2.com/documentation-escape-codes.html
changing terminal
to window
fixes the script to work (as of May 2024)
set_term_bgcolor(){
local R=$1
local G=$2
local B=$3
/usr/bin/osascript <<EOF
tell application "iTerm"
tell the current window
tell the current session
set background color to {$(($R*65535/255)), $(($G*65535/255)), $(($B*65535/255))}
end tell
end tell
end tell
EOF
}
FWIW, I added the above function to my initial script and I run the function manually to set the background color.
Reference to iTerm applescript support - https://iterm2.com/documentation-scripting.html
Added some predefined colors from the Tailwind palette - https://tailwindcolor.com/
function setbg(){
local R=$1
local G=$2
local B=$3
[ -z "$1" ] && { echo "Colors: red, orange, amber, yellow, lime, green, cyan, sky, indigo, purple, pink, grey"; return; }
[ "$1" = "red" ] && { R=254 G=226 B=226; }
[ "$1" = "orange" ] && { R=255 G=237 B=213; }
[ "$1" = "amber" ] && { R=254 G=243 B=199; }
[ "$1" = "yellow" ] && { R=254 G=249 B=195; }
[ "$1" = "lime" ] && { R=236 G=252 B=203; }
[ "$1" = "green" ] && { R=220 G=252 B=231; }
[ "$1" = "cyan" ] && { R=207 G=250 B=254; }
[ "$1" = "sky" ] && { R=224 G=242 B=254; }
[ "$1" = "indigo" ] && { R=224 G=231 B=255; }
[ "$1" = "purple" ] && { R=243 G=232 B=255; }
[ "$1" = "pink" ] && { R=252 G=231 B=243; }
[ "$1" = "gray" ] && { R=243 G=244 B=246; }
# echo "Setting to ($R, $G, $B)"
/usr/bin/osascript <<EOF
tell application "iTerm"
tell the current window
tell the current session
set background color to {$(($R*65535/255)), $(($G*65535/255)), $(($B*65535/255))}
end tell
end tell
end tell
EOF
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can use profiles to make this work easier. See https://gist.github.com/mihigh/ead75ff55756680ae7cd