Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

On a Mac, within the shell, how can I tell that I have a GUI?

Because you (lovely) people are always so curious about posters' original intents, here's mine:

If I'm on a Mac and have a GUI (as opposed to, say, being on an ssh session), I want to set my $EDITOR to mate_wait. (And go with vim otherwise.)

And, you have an answer for that. I do too. It even works. Here. Sometimes.

So I want you to fiercely scrutinize it:

Skip intro

I can tell that I'm on a Mac by checking:

[ `uname` = 'Darwin' ]

And I think I can sort of tell that I have a GUI by checking:

[ "$TERM_PROGRAM" = 'Apple_Terminal' ]
  # or
[ "$DISPLAY" ]

Now, it's theoretically possible that I have an Aqua-less OpenDarwin setup running X11. It's also possible that I'm running fully lickable Mac GUI, yet using another terminal application.

And then there's the mind-bending possibility that I'm running xterm within Apple's X11 running on top of the OS X GUI. In which case I'd still want mate_wait as $EDITOR.

For OCD's sake, I'd like my checks to be as precise as possible.

So, please, un-reckless-fy my code.

like image 219
kch Avatar asked Sep 11 '25 11:09

kch


1 Answers

You can ask launchctl what is managing the current session.

if [ "$(launchctl managername)" == Aqua ]; then
    EDITOR="matew";
else
    EDITOR="vim";
fi;
like image 59
Glyph Avatar answered Sep 13 '25 05:09

Glyph