4

Is there a way to bring GUI applications to the foreground when starting them from within Terminal on Mac OS X? If I run

/Applications/TextEdit.app/Contents/MacOS/TextEdit

in Terminal, TextEdit opens in the background. This is quite annoying if you're using "make && ./run" when developing GUI applications.

jnsg
  • 88
  • 1
  • 5

3 Answers3

6

There seems to be even shorter variant:

> open -a TextEdit

brablc
  • 1,621
  • 18
  • 17
6

At least for TextEdit and similar GUI apps, open will work:

> open /Applications/TextEdit.app
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • 1
    Thanks! I've tried to use `open` before, but didn't realize I can open the whole bundle, too. If you use `open /Applications/TextEdit.app/Contents/MacOS/TextEdit`, a new terminal window is launched and the program is again put to the background. – jnsg Oct 15 '11 at 17:59
  • 1
    @jnsg, I'd never thought of using `open` with the executable path. That's interesting behavior. It makes sense, though: If you open an executable from Finder, it will run it in Terminal. – Chris Page Jul 02 '12 at 22:06
  • Is there a way to use `open` or some other command that (a) brings the app into the foreground and (b) doesn't exit once the app is launched? – Spencer Williams Jan 03 '15 at 01:46
  • Now if only it would inherit the child process's stdout/stderr output streams, then I could use it to spawn a foreground process from Node.js and capture output. – Myk Melez Mar 01 '17 at 17:42
  • @SpencerWilliams I just posted an answers for accomplish what you asked for – jviotti Nov 29 '22 at 01:41
0

The answers described in this post use open(1) with its default options. This has two limitations that might be important in your use case:

  • open runs the application bundle as a detached process
  • open does not pipe standard I/O back to your script/shell

To solve these:

  • open takes a -W option for open to wait on the application to quit
  • open takes --stdout and --stderr option that you can pipe to your shell (or script) TTY

For example:

open -W --stdout $(tty) --stderr $(tty) <path/to/Bundle.app>

I wrote some more about this here: https://www.jviotti.com/2022/11/28/launching-macos-applications-from-the-command-line.html

jviotti
  • 17,881
  • 26
  • 89
  • 148