Skip to content

Instantly share code, notes, and snippets.

@BugRoger
Created January 21, 2011 16:02
Show Gist options
  • 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
@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