Tag Archives: iterm2

Docker Kitematic (Beta) and iTerm2 3.0 Beta

I guess using everything in beta can cause issues, but whatever.

Kitematic has an ‘Exec’ option for containers which opens a terminal connected to that container. If you have iTerm2 v3.0 beta installed, you’ve probably noticed this opens in Terminal.app instead of iTerm2.

The button in Kitematic executes a bash script at

/Applications/Docker/Kitematic (Beta).app/Contents/Resources/resources/terminal

. The bash script executes AppleScript, and the AppleScript syntax for iTerm2 in 3.0 has changed.

Here’s a replacement for that terminal bash script, with the new iTerm2 syntax (minus extra error handling).

#!/bin/bash

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
CMD="clear && $*"

ITERM_EXISTS=`osascript <<EOF
set doesExist to false
try
    do shell script "osascript -e 'exists application \"iTerm2\"'"
    set doesExist to true
end try
return doesExist
EOF`

function open_iterm () {
  osascript > /dev/null <<EOF
  tell application "iTerm2"
    activate
    set newWindow to (create window with default profile)
    select first window

    tell current window
      tell current session
        write text "bash -c \"$CMD\""
      end tell
    end tell
  end tell
EOF
}

function open_terminal () {
  osascript > /dev/null <<EOF
  tell application "Terminal" to activate
  delay 0.4
  tell application "System Events" to keystroke "t" using command down
  tell application "Terminal"
    do script "bash -c \"$CMD\"" in window 1
  end tell
EOF
}

if [ "$ITERM_EXISTS" == "true" ]; then
  open_iterm "$@" || open_terminal "$@"
else
  open_terminal "$@"
fi

This may not be 100% correct, but it works.

Flattr this!