Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Execute shell-script from Unity script (OSX TTS)

Discussion in 'Scripting' started by akk, Nov 25, 2012.

  1. akk

    akk

    Joined:
    Nov 25, 2012
    Posts:
    2
    Hi,

    I'm using Unity 3.5.6 on a Mac and am trying to write a Unity script that runs a shell-script in the OSX terminal, in order to use OSX's built in text-to-speech function. I've tried adding the following to the update function:

    A: System.Diagnostics.Process.Start("/applications/utilities/terminal.app", "sh talk.sh");

    The script talk.sh contains:
    osascript -e 'say "halløjsa" using "Ida"'

    When i run it, nothing happens. However if i delete the argument "sh talk.sh" a terminal window opens. If i replace with Start("/applications/safari", "http://google.com") it also works fine.

    I've also tried:
    B: System.Diagnostics.Process.Start("/applications/utilities/terminal.app", "say hi");
    C: System.Diagnostics.Process.Start("usr/bin/osascript", "-e say hi");

    But still no luck. Any help or suggestions would be much appreciated.

    PS: I've also tried opening a browser with google translate's tts, which somehow works, but it's restricted to 100 characters.
     
  2. Landern

    Landern

    Joined:
    Dec 21, 2008
    Posts:
    354
    Code (csharp):
    1.  
    2. using System.Diagnostics;
    3. ...
    4. ProcessStartInfo proc = new ProcessStartInfo("open", "sh talk.sh \"Hello and stuff\"");
    5. proc.UseShellExecute = false;
    6. Process.Start(proc);
    7. ...
    8.  
    This hasn't been tested, but it uses the open command, i don't think you have to specify the terminal.app bundle as the executing application.

    Apple's Docs for open here.
     
    Last edited: Nov 25, 2012
  3. akk

    akk

    Joined:
    Nov 25, 2012
    Posts:
    2
    Thanks! I got it to work using the open command. However i would like to start the process in either a hidden- or minimised window, but it seems to ignore ProcessWindowStyle.Minimized. The way it is now, it will open the terminal window on top of the unity application while pausing the game.


    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Diagnostics;
    4.  
    5. public class talk : MonoBehaviour {
    6.  
    7.     void Start () {
    8.         ProcessStartInfo proc = new ProcessStartInfo();
    9.         proc.FileName = "open";
    10.         proc.WorkingDirectory = "/users/myUserName";
    11.         proc.Arguments = "talk.sh";
    12.         proc.WindowStyle = ProcessWindowStyle.Minimized;
    13.         proc.CreateNoWindow = true;
    14.         Process.Start(proc);
    15.         UnityEngine.Debug.Log("Halløjsa");
    16.     }
    17.    
    18.     void Update () {
    19.    
    20.     }
    21. }
     
    tomlong74 likes this.
  4. L4Z3RC47

    L4Z3RC47

    Joined:
    Jun 21, 2015
    Posts:
    46
    This should work without the need for a pre written script and does not open the terminal window

    Code (CSharp):
    1. private IEnumerator Speak (string command){
    2.    
    3.         ProcessStartInfo startInfo = new ProcessStartInfo("/bin/bash");
    4.         startInfo.WorkingDirectory = "/";
    5.         startInfo.UseShellExecute = false;
    6.         startInfo.RedirectStandardInput = true;
    7.         startInfo.RedirectStandardOutput = true;
    8.  
    9.         Process process = new Process();
    10.         process.StartInfo = startInfo;
    11.         process.Start();
    12.  
    13.         process.StandardInput.WriteLine("say " + command);
    14.         process.StandardInput.WriteLine("exit");  // if no exit then WaitForExit will lockup your program
    15.         process.StandardInput.Flush();
    16.  
    17.         string line = process.StandardOutput.ReadLine();
    18.        
    19.         process.WaitForExit();
    20.         yield return null;
    21.     }
     
  5. tammyhuang

    tammyhuang

    Joined:
    Jun 14, 2016
    Posts:
    1
    How can I change the above code to do something like a bash command "flac sample.wav" on my mac system?