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?
3 回答
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}.
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.
Marc Liyanage does this neatly with "term", osascript tell application "Terminal":
http://www.entropy.ch/blog/Mac+OS+X/2005/02/28/Terminal_tricks_8220_term_8221_and_8220_clone_8221.html
(On "typical users": there are different universes, each finding the others strange.
Coming from Unix, I'm used to a process or "| pipe args" anywhere a file can be used;
this helps software componentry immensely.
But open -a does files only -- different universe).