Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Launching macOS terminal and passing commands

Discussion in 'Scripting' started by SpiderJones, Feb 22, 2017.

  1. SpiderJones

    SpiderJones

    Joined:
    Mar 29, 2014
    Posts:
    223
    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!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    Google just a little longer and I think you want the .Arguments property, but I've not tried it personally.
     
  3. SpiderJones

    SpiderJones

    Joined:
    Mar 29, 2014
    Posts:
    223
    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);
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    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
     
  5. SpiderJones

    SpiderJones

    Joined:
    Mar 29, 2014
    Posts:
    223
    Yes! That did the trick. Thanks!
     
    Kurt-Dekker likes this.
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    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.
     
  7. emathew

    emathew

    Joined:
    Jul 31, 2017
    Posts:
    13
    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...
     
  8. Dizlen

    Dizlen

    Joined:
    Sep 30, 2020
    Posts:
    1
    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.
     
  9. sean_unity22

    sean_unity22

    Joined:
    Jun 6, 2021
    Posts:
    1
    Anyone figure this out yet?
     
  10. miaox_aws

    miaox_aws

    Joined:
    Jan 17, 2021
    Posts:
    1
    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:

    Code (CSharp):
    1. new ProcessStartInfo
    2. {
    3.   FileName = "/System/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal",
    4.   Arguments = "myscript.sh"
    5. }
    2. Write a apple script to open terminal and execute your scripts:

    Code (CSharp):
    1. string reOpenTerminalScript = $"tell application \\\"Terminal\\\" to if not (exists window 1) then reopen";
    2. string activateTerminalScript = $"tell application \\\"Terminal\\\" to activate";
    3. string runMyScript = $"tell application \\\"Terminal\\\" to do script \\\"echo hello\\\" in window 1";
    4. string osaScript = $"osascript -e \'{reOpenTerminalScript}\' -e \'{activateTerminalScript}\' -e \'{runMyScript}\'";
    5. string bashCommand = $" -c \"{osaScript}\"";
    6.  
    7. processStartInfo = new ProcessStartInfo
    8. {
    9.     UseShellExecute = false,
    10.     FileName = "/bin/bash",
    11.     CreateNoWindow = false,
    12.     Arguments = bashCommand
    13. };
    Credit:
    * https://stackoverflow.com/questions/10667800/using-quotes-in-a-applescript-string/10668503
    * https://stackoverflow.com/questions/34952557/why-is-my-applescript-opening-multiple-terminal-windows
     
    samol2014 likes this.
  11. samol2014

    samol2014

    Joined:
    Oct 17, 2019
    Posts:
    3

    Thanks a lot!

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