Skip to content

Instantly share code, notes, and snippets.

@BugRoger
Created January 21, 2011 16:02
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save BugRoger/789887 to your computer and use it in GitHub Desktop.
Save BugRoger/789887 to your computer and use it in GitHub Desktop.
Changes iTerm2's background color based on host configuration
#!/bin/bash
# Installation:
# 1. Save this script to /some/bin/ssh-background
# 2. chmod 755 /some/bin/ssh-background
# 3. alias ssh=/some/bin/ssh-background
# 4. Configure your host colors below.
set_color() {
local HEX_FG=$1
local HEX_BG=$2
local OPACITY=$3
local FG_R=`echo $HEX_FG | sed 's/../0x&,/g' | awk -F "," '{printf("%d",$1 * 257)}'`
local FG_G=`echo $HEX_FG | sed 's/../0x&,/g' | awk -F "," '{printf("%d",$2 * 257)}'`
local FG_B=`echo $HEX_FG | sed 's/../0x&,/g' | awk -F "," '{printf("%d",$3 * 257)}'`
local BG_R=`echo $HEX_BG | sed 's/../0x&,/g' | awk -F "," '{printf("%d",$1 * 257)}'`
local BG_G=`echo $HEX_BG | sed 's/../0x&,/g' | awk -F "," '{printf("%d",$2 * 257)}'`
local BG_B=`echo $HEX_BG | sed 's/../0x&,/g' | awk -F "," '{printf("%d",$3 * 257)}'`
/usr/bin/osascript <<EOF
tell application "iTerm"
tell current session of current terminal
set foreground color to {$FG_R, $FG_G, $FG_B}
set background color to {$BG_R, $BG_G, $BG_B}
set transparency to "$OPACITY"
end tell
end tell
EOF
}
if [[ "$@" =~ host0.example.com ]]; then
set_color ffffff 330000 0.2
elif [[ "$@" =~ host1.example.com ]]; then
set_color ffffff 000033 0.2
fi
ssh $@
set_color ffffff 000000 0.2
@therollingtester
Copy link

Great stuff, thanks!

@mustdobetter
Copy link

Just what I've been looking for, thanks!

@dpmcnevin
Copy link

This is very helpful! Thank you!

@pankesh
Copy link

pankesh commented Dec 17, 2014

Spot on.. just what i was planning to put together but didn't know how to "tell application" Nice trick!

@awol
Copy link

awol commented Sep 2, 2015

Thanks, I've been hankering for something like this for a while. I made a fork that adds a couple of features I have found convenient. Maybe they're useful to others as well. Cheers...

@fondberg
Copy link

Thanks! great stuff.
If anyone is interested I have expanded it for some more func for normal terminal control.

#!/bin/bash
# Installation:
# Adapted for some more control by Niklas Fondberg niklas.fondberg@gmail.com
#1. Save this script to /some/bin/itermControl
#2. chmod 755 /some/bin/itermControl
#3. Configure your defaults in the defaults section

set_color() {
  local HEX_FG=$1
  local HEX_BG=$2
  local OPACITY=$3

  local FG_R=`echo $HEX_FG | sed 's/../0x&,/g' | awk -F "," '{printf("%d",$1 * 257)}'`
  local FG_G=`echo $HEX_FG | sed 's/../0x&,/g' | awk -F "," '{printf("%d",$2 * 257)}'`
  local FG_B=`echo $HEX_FG | sed 's/../0x&,/g' | awk -F "," '{printf("%d",$3 * 257)}'`
  local BG_R=`echo $HEX_BG | sed 's/../0x&,/g' | awk -F "," '{printf("%d",$1 * 257)}'`
  local BG_G=`echo $HEX_BG | sed 's/../0x&,/g' | awk -F "," '{printf("%d",$2 * 257)}'`
  local BG_B=`echo $HEX_BG | sed 's/../0x&,/g' | awk -F "," '{printf("%d",$3 * 257)}'`

  /usr/bin/osascript <<EOF
tell application "iTerm"
   tell current session of current terminal
      set foreground color to {$FG_R, $FG_G, $FG_B}
      set background color to {$BG_R, $BG_G, $BG_B} 
      set transparency to "$OPACITY" 
   end tell
end tell
EOF
}

setTerminalText () {
  if [ -z "$1" ]; then
    echo "setTerminaltext 1 is not empty"
    TXT=$1
  fi
  unset PROMPT_COMMAND
  echo -ne "\033]0;"$TXT"\007"
  echo "Changing tab title to $TXT"
  #export PROMPT_COMMAND='echo -ne "\033]0;${TXT}\007"'
}
setTabColor() {
  if [ -z "$1" ]; then
    echo "setTabColor 1 is not empty"
    TAB=$1
  fi
  echo "Changing tab color to $TAB"
  echo -ne "\033]6;1;bg;${TAB};brightness;255\a"
}

usage() {
  echo "Usage: itermControl [options...]"
  echo "Options: "
  echo "-f, --foreground        Hex color of foreground in the format of FFFFFF"
  echo "-b, --background        Hex color of background in the format of FFFFFF"
  echo "-o  --opacity           Opcacity of the terminal 0.0 - 1.0"
  echo "NOTE: if any of the options f,b or o are set and some are omitted default values for the omitted are used"
  echo "-c, --tabcolor          Color of tab title in the format of a text color (red, blue, green, white, et.c"
  echo "-t, --title             Title text of the terminal"
  echo "-d, --default           Use default values and set everything"
  exit 0
}


if [ -z "$1" ]; then
  usage
fi

#DEFAULTS
FG="ffeeff"
BG="003300"
OP="0.2"
TAB="green"
TXT="Special term"
SETCOLOR=0


while [[ $# > 0 ]]
  do
  key="$1"
  echo "here key $key"
  case $key in
      -f|--foreground)
        FG="$2"
        SETCOLOR=1
        shift # past argument
        ;;
      -b|--background)
        SETCOLOR=1
        BG="$2"
        shift # past argument
        ;;
      -o|--opacity)
        SETCOLOR=1
        OP="$2"
        shift # past argument
        ;;
      -c|--tabcolor)
        TAB="$2"
        setTabColor $TAB
        shift # past argument
        ;;
      -t|--title)
        TXT="$2"
        setTerminalText $TXT
        shift # past argument
        ;;
     -d|--default)
        setTerminalText $TXT
        setTabColor $TAB
        SETCOLOR=1
        shift # past argument
        ;;
      *)
        echo "Unknown option $2"
        usage
        # unknown option
      ;;
  esac
  shift # past argument or value
done

if [ $SETCOLOR -ne 0 ] ; then
  echo "calling set_color with $FG $BG $OP"
  set_color $FG $BG $OP
fi

@mihigh
Copy link

mihigh commented Dec 11, 2015

You can use profiles to make this work easier. See https://gist.github.com/mihigh/ead75ff55756680ae7cd

@shyazusa
Copy link

Fix '59:67: syntax error: end of lineがあるべきところですがidentifierが見つかりました。 (-2741)'

    /usr/bin/osascript <<EOF
  tell application "iTerm"
-   tell current session of current terminal
+   tell current session of first window
      set foreground color to {$FG_R, $FG_G, $FG_B}
      set background color to {$BG_R, $BG_G, $BG_B}
      set transparency to "$OPACITY"
    end tell
  end tell
  EOF
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment