Superloopy Logo

Change Terminal background colour on OS X via ssh

I show how to change colours automatically when logging into certain remote machines via SSH

I spend most of my work days in Emacs, Slack, Safari & Terminal. Some of the Terminals I've got open, such as those connected to a machine in the production environment, requires a more elevated attention than others. It would be nice if these "high risk" terminals were visually distinct. A colleague shared How to change terminal colors change when connecting to SSH hosts and it inspired me to finally implement this.

The solution presented there does not work on OS X Terminal, so I had to roll my own. My first hurdle was how to change the Terminal background from a script. The only solution I could find was with AppleScript. The next issue I had was that the solution given in that post changes the background for all tabs in that terminal window, which is not what I want. I can't remember where I found how to affect only one tab, but here is the code I'm using now:

on run desiredColour
    if length of desiredColour > 0
        set colour to desiredColour
        # The RGB colour of the "Novel" profile I'm using;
        # This is the default I want to get back to.
        set colour to { 52007, 48990, 29970 }

   tell application "Terminal"
        tell selected tab of front window
          set background color to colour
     end tell
  end tell
end run

I put the above in ~/Dotfiles/termcolours.scpt, and invoke it from the commandline like so:

osascript ~/Dotfiles/termcolours.scpt red

That changes the background colour of the current tab to red. (Orange, yellow, blue, green also works. Try it! Go wild!) Calling the script with no arguments resets the terminal background back to its default colour:

osascript ~/Dotfiles/termcolours.scpt

Now, after adding PermitLocalCommand yes to my /etc/ssh/ssh_config I can put the following in my ~/.ssh/config:

    LocalCommand osascript ~/Dotfiles/termcolours.scpt red

The current Terminal tab now changes to red when I ssh to that machine. Result!

Ah, but wait! When I exit the remote machine the background of that tab is not reset to its original colour. For that, if you use Bash/Zsh you could just use the solution from the post I linked to above. But I use fish, and it uses functions instead of aliases. I put the following in ~/.config/fish/functions/

function ssh --description 'wrapper around ssh to reset term colours after exit'
        /usr/bin/ssh $argv
        osascript ~/Dotfiles/termcolours.scpt

And there you have it!

Date: 2016-09-09

Author: Stig Brautaset

Created: 2017-06-10 Sat 21:59