Reputation:
How can I open a new terminal from C ++ code and write inside it. I know how to open new terminal by using system command (system("/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal")), but do not know how to write string in it ? I'm working on an operating system mac os.
In Linux you can do so
std :: string cmd = "gnome-terminal-x sh-c 'ls-l; exec bash'";
system (cmd.c_str ());
how to do it in the mac os ?
Upvotes: 1
Views: 4616
Reputation: 5431
Your basic mechanism of calling system()
should still work, you just need a different command.
One way to do this is by running AppleScript from the command line via osascript
. You can use the "AppleScript Editor" application (and use the Library command in its Window menu) to learn more about all the commands that can be given to programs in this way.
For example, to make the Mac Terminal run top
, I could invoke this command line:
/usr/bin/osascript -e 'tell application "Terminal" to do script "top"'
Similarly, if I'd already written an entire file of commands to run, I could give it a .command
extension and ask Terminal to open the file instead:
/usr/bin/osascript -e 'tell application "Terminal" to open "/Users/me/Desktop/MyFile.command"'
Upvotes: 0