Launching macOS terminal and passing commands

Hi,

I’m able to launch the Terminal, but I can’t figure out how to pass commands to it. The code below will open the Terminal, but any suggestions has to how I pass a command to the terminal?

ProcessStartInfo proc = new ProcessStartInfo();
proc.FileName = “/Applications/Utilities/Terminal.app”;
Process.Start(proc);

Thanks for any help!

Google just a little longer and I think you want the .Arguments property, but I’ve not tried it personally.

Hi,

Yes I tried the argument property but then it tries to open an app directly and not via a command line. And this gives a warning from Mac OS. The binary must be opened via a command line in the terminal.

ProcessStartInfo proc = new ProcessStartInfo();
proc.FileName = “/Applications/Utilities/Terminal.app”;
proc.Arguments = “/Users/[user]/Desktop/ARUnity5-5.3.2-tools-osx/bin/calib_camera”;
Process.Start(proc);

Ah, gotcha. Look inside Terminal.app (right click to Show Package Contents) and then dig into Contents/MacOS and there should be an actual executable binary. Pass that as your executable filename, then I think the args will work.

BTW, terminal is contained in under /Applications/Utilities

Yes! That did the trick. Thanks!

1 Like

That’s actually a handy way to get two instances of an app running on MacOSX. I have a bash alias (simply ‘u’) that links to Unity’s equivalent binary and I can launch as many instances of Unity as I want, as long as each points to a different project. Handy when you just want to fire up another project to see how you did something, without leaving your current project.

This solution still doesnt work for me… What I have…

ProcessStartInfo proc = new ProcessStartInfo();
proc.FileName = “/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal”;
proc.Arguments = “ls”;
Process.Start(proc);

The terminal window opens up, but nothing else executes…

Having the same problem as well. Any chance you solved this…? Seems like no matter what I’ve tried it just won’t write anything to terminal.

Anyone figure this out yet?

Same here, I cannot find a good way to Open a Terminal and run a script like you could easily do in Windows.

I found 2 workaround for this:

  1. Write a shell script and execute that instead:
new ProcessStartInfo
{
  FileName = "/System/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal",
  Arguments = "myscript.sh"
}
  1. Write a apple script to open terminal and execute your scripts:
string reOpenTerminalScript = $"tell application \\\"Terminal\\\" to if not (exists window 1) then reopen";
string activateTerminalScript = $"tell application \\\"Terminal\\\" to activate";
string runMyScript = $"tell application \\\"Terminal\\\" to do script \\\"echo hello\\\" in window 1";
string osaScript = $"osascript -e \'{reOpenTerminalScript}\' -e \'{activateTerminalScript}\' -e \'{runMyScript}\'";
string bashCommand = $" -c \"{osaScript}\"";

processStartInfo = new ProcessStartInfo
{
    UseShellExecute = false,
    FileName = "/bin/bash",
    CreateNoWindow = false,
    Arguments = bashCommand
};

Credit:

1 Like

Thanks a lot!

It really helped me. For those who struggling with it: put a lot of attention to backslashes and spaces.