Starting process in new Terminal window on Mac

10,391

Solution 1

open -a Terminal.app $(which program) gets a new terminal running the specified program (assuming you're using bash).

You can use execve() (possible after a fork()) to acheive the same thing in compiled code without knowing any Apple APIs (I imagine there is a proper way to do this...).

Read man open.

Edit: you don't need to specify the path to Terminal.app (the finder can figure that out).


If you have X running, it is even easier: just spawn a new xterm with xterm -e program &.

Read man xterm (which will take longer...).


I'll second Chris about the correct use (or lack thereof) of CLI for ordinary mac programs. In my buisness this is expected but the typical user will be {confused|angry|unhappy}.

Solution 2

The Terminal (Terminal.app, what you're referring to as "the console") is just another user-level application on Mac OS X, not a capability of the operating system. There is no direct way in the various APIs available on Mac OS X to just start an executable within a new Terminal window.

However, I believe you can open an executable with Terminal as if it was a document — whether in code or as a user — and it will run in a new session. However, this is not a normal Mac OS X user experience and should not generally be used in Mac software you're going to deliver to end users.

Mac OS X applications are applications. It's fine to provide tools that advanced users can interact with via Terminal, but Terminal is in no way, shape or form a substitute for a real application when delivering software to end users.

I'll add to this that if you're using Cocoa, you can use the NSTask class to start and interact with another process very easily.

Share:
10,391
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    On Windows I can do CreateProcess(..., CREATE_NEW_CONSOLE, ...) and my child process (which is console app, not GUI) will be launched in a new window. What is the easiest way to emulate this on Mac OS?