Run certain apps based on login location

Mar 30, '05 09:21:00AM

Contributed by: taxi

I have a few aliases and functions running in my ~/.profile, and some of them run Mac OS X applications -- for instance, I like to edit files in SubEthaEdit, but I like to just use the word edit as the command to start the process. This is all well and good, until I'm remotely accessing a command line only, and try to edit a text file. If I'm logged into the Mac, up pops SubEthaEdit, and if I'm not logged in, I see this:

kCGErrorRangeCheck : Window Server communications from outside of session allowed for root and console user only INIT_Processeses(), could not establish the default connection to the WindowServer.Abort trap

Not much fun at all. So I wrote a function that will run the right program if I am remotely logged in. I found that Terminal sets the environment variable TERM_PROGRAM to Apple_Terminal, which I can use to tell where I'm working.

Here's the function:

function edit
{
  if test `echo $TERM_PROGRAM` == 'Apple_Terminal' ; then
      open -a 'SubEthaEdit.app' "$*" &
  else
      pico "$*"
  fi
}
So far so good. But when I exit pico from my other terminal, I see an error:
-bash: test: ==: unary operator expected
What's happening is that the `echo $TERM_PROGRAM` is displaying "" (an empty string), which test doesn't like on the left hand side of a == operator. So the first lines of my ~/.profile are now:
if [[ $TERM_PROGRAM == 'Apple_Terminal' ]]; then
 export TERM_PROGRAM='Unknown'
fi
This took me a while to twig to -- for ages, I was just happy to have the error appear! The only issue with this is if I telnet from my iMac (where the variable is set) into the same machine, I then lose the ability to automatically run the graphical programs set up in the functions. The other programs I use differently according to where I am include: I also have a function called browse that opens the current (or supplied) directory in the Finder. I'd love to be able to tell Windows Explorer to open this, but that would require some heavy duty inter-machine communication.

Comments (4)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20050119061627975