Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Run certain apps based on login location UNIX
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:
  • appswitch - use as a replacement for ps, kill, and to show/hide running programs.
  • SubEthaEdit - use as edit, and sedit (edit as Super User).
  • openman - a new window for each man page, allowing them to stay open while I keep working.
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.
    •    
  • Currently 2.80 / 5
  You rated: 3 / 5 (5 votes cast)
 
[6,539 views]  

Run certain apps based on login location | 4 comments | Create New Account
Click here to return to the 'Run certain apps based on login location' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Run certain apps based on login location
Authored by: Brock Lee on Mar 30, '05 10:17:43AM

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.

You can put double quotes around the backquoted expression to deal with this. So here's your code with the double quotes added on the third line. It's important to remember that within double quotes, both back-quotes and dollar signs still do their magic (which is not true for single quotes).

function edit
{
  if test "`echo $TERM_PROGRAM`" == 'Apple_Terminal' ; then
      open -a 'SubEthaEdit.app' "$*" &
  else
      pico "$*"
  fi
}



[ Reply to This | # ]
Run certain apps based on login location
Authored by: Titanium Man on Mar 30, '05 12:18:31PM

Or you can use double brackets [[ ]] to test (rather than single brackets [ ]):

edit() {
if [[ ${TERM_PROGRAM} == 'Apple_Terminal' ]]; then
open -a 'SubEthaEdit.app' "$*" &
else
pico "$*"
fi
}



[ Reply to This | # ]
Standard shell programming technique
Authored by: TrumpetPower! on Mar 30, '05 10:19:49PM

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.

There is a standard technique to avoid just this problem when writing shell scripts. The solution is to prepend a string—usually “X”—to both strings to be tested.

For example:

if [ X"${foo}" == X"bar" ]; then
    echo "foo is bar";
else
    echo "foo is not bar";
fi

That should be portable across pretty much any Bourne-style shell.

Cheers,

b&



[ Reply to This | # ]
Run certain apps based on login location
Authored by: wgscott on Mar 30, '05 11:36:10PM
If anyone wants some similar functions for zsh, please feel free to help yourself to

[ Reply to This | # ]