AppleScript | Dmytro Shteflyuk's Home https://kpumuk.info In my blog I'll try to describe about interesting technologies, my discovery in IT and some useful things about programming. Wed, 23 Jun 2010 18:48:13 +0000 en-US hourly 1 https://wordpress.org/?v=6.6.1 How to show SSH host name on the iTerm’s background https://kpumuk.info/mac-os-x/how-to-show-ssh-host-name-on-the-iterms-background/ https://kpumuk.info/mac-os-x/how-to-show-ssh-host-name-on-the-iterms-background/#comments Fri, 01 May 2009 09:30:50 +0000 http://kpumuk.info/?p=612 How many ssh session do you open usually? In Scribd we have about 50 machines, and most of the time I have to connect to several of them to do my work. But there is a big problem — it’s hard to distinguish among different tabs in iTerm. Of course, you can see the host […]

The post How to show SSH host name on the iTerm’s background first appeared on Dmytro Shteflyuk's Home.]]>
iTerm How many ssh session do you open usually? In Scribd we have about 50 machines, and most of the time I have to connect to several of them to do my work. But there is a big problem — it’s hard to distinguish among different tabs in iTerm. Of course, you can see the host where you connected to in the tab title, but the title is really small, low contrast, etc. So I had a dream since I’ve started using Mac — to get current host written with large letters on the background.

iTerm has a great support of AppleScript. Looking through the commands list I’ve found a really useful one: background image path. The last thing to figure out is how to create an image with the current host name, and I decided to use ImageMagick. So, let’s do it!

First, you need to install ImageMagick. You can use binary distribution, but I’d prefer the MacPorts version:

1
port install ImageMagick

Let’s create a test image:

1
2
3
convert -size "570x342" xc:black -gravity "NorthEast" -fill "#662020" \
        -family "Georgia" -style "Normal" -pointsize "60" -antialias \
        -draw "text 20,10 'kpumuk.info'" "/tmp/iTermBG.png"

Here is the result and it looks pretty good!

Image with hostname

Ok, now the most interesting part. I’ve created a bash script in ~/bin/ssh, which sets the background before calling ssh and clears it on exit. I suppose you have this folder specified before /usr/bin in your .bash_profile:

1
export PATH=$HOME/bin:/opt/local/bin:$PATH

Here is the script (the latest version could be found in a GitHub repository here):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/bin/bash

# SSH with host name and IP address in background (only in iTerm.app)

# First, check to see if we have the correct terminal!
if [ "$(tty)" == 'not a tty' ] || [ "$TERM_PROGRAM" != "iTerm.app" ] ; then
  /usr/bin/ssh "$@"
  exit $?
fi

function __calculate_iterm_window_dimensions {
  local size=( $(osascript -e "tell application "iTerm"
    get bounds of window 1
  end tell"
| tr ',' ' ') )

  local x1=${size[0]} y1=${size[1]} x2=${size[2]} y2=${size[3]}
  # 15px - scrollbar width
  local w=$(( $x2 - $x1 - 15 ))
  # 44px - titlebar + tabs height
  local h=$(( $y2 - $y1 - 44))
  echo "${w}x${h}"
}

# Console dimensions
DIMENSIONS=$(__calculate_iterm_window_dimensions)

BG_COLOR="#000000"       # Background color
FG_COLOR="#662020"       # Foreground color
GRAVITY="NorthEast"      # Text gravity (NorthWest, North, NorthEast,
                         # West, Center, East, SouthWest, South, SouthEast)
OFFSET1="20,10"          # Text offset (host name)
OFFSET2="20,80"          # Text offset (ip address)
FONT_SIZE="60"           # Font size in points
FONT_STYLE="Normal"      # Font style (Any, Italic, Normal, Oblique)
# Font path
FONT="$HOME/.bash/resources/SimpleLife.ttf"

HOSTNAME=`echo $@ | sed -e "s/.*@//" -e "s/ .*//"`

output=`dscacheutil -q host -a name $HOSTNAME`
RESOLVED_HOSTNAME=`echo -e "$output"|grep '^name:'|awk '{print $2}'`
RESOLVED_IP=`echo -e "$output"|grep '^ip_address:'|awk '{print $2}'`

function set_bg {
  local tty=$(tty)
  osascript -e "
    tell application "
iTerm"
      repeat with theTerminal in terminals
        tell theTerminal
          try
            tell session id "
$tty"
              set background image path to "
$1"
            end tell
          on error errmesg number errn
          end try
        end tell
      end repeat
    end tell"

}

on_exit () {
  if [ ! -f /tmp/iTermBG.empty.png ]; then
    convert -size "$DIMENSIONS" xc:"$BG_COLOR" "/tmp/iTermBG.empty.png"
  fi
  set_bg "/tmp/iTermBG.empty.png"
  rm "/tmp/iTermBG.$$.png"
}
trap on_exit EXIT

convert \
  -size "$DIMENSIONS" xc:"$BG_COLOR" -gravity "$GRAVITY" -fill "$FG_COLOR" \
  -font "$FONT" -style "$FONT_STYLE" -pointsize "$FONT_SIZE" -antialias \
  -draw "text $OFFSET1 '${RESOLVED_HOSTNAME:-$HOSTNAME}'" \
  -pointsize 30 -draw "text $OFFSET2 '${RESOLVED_IP:-}'" \
  "/tmp/iTermBG.$$.png"
set_bg "/tmp/iTermBG.$$.png"

/usr/bin/ssh "$@"

The script is big, but very simple. In lines 6-9 script detects the current terminal application, and if it is not iTerm — skips the background generation. Also it checks if the SSH connection was requested from an interactive command line or from an another script (e.g., from git). If you want to see what host git tries to connect to — remove the tty check. Function on lines 11-22 calculates the iTerm window size, so generated background will not be stretched. If you have disabled scrollbars — remove - 15 on line 18. Configuration options determining how host name will be printed on your background are on lines 27-36. Lines 38-42 normalize host name and resolve IP address. Function on lines 44-59 changes background of the iTerm tab where ssh command was invoked from using AppleScript. It’s tricky a little, but still easy to understand. Lines 61-68 trap script exit. They handle special case when you kill ssh session, and get background image for non-ssh console. Lines 70-76 generate background image using ImageMagick.

Let’s see it in action:

So now I can stop shaking with fear when doing some destructive things in console — I know which host exactly I’m trying to kill. I hope you will find this post useful.

Changelog

05/01/2009: Updated on_exit function to fix bug with scrolling after ssh session being closed.

05/03/2009: Added some variables to customize how host name will look.

06/23/2010: Better console size detection, added normalization of host name, and IP address below the hostname, iTerm detection (to prevent errors in Terminal.app).

The post How to show SSH host name on the iTerm’s background first appeared on Dmytro Shteflyuk's Home.]]>
https://kpumuk.info/mac-os-x/how-to-show-ssh-host-name-on-the-iterms-background/feed/ 9