Execute shell-script from Unity script (OSX TTS)

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.

using System.Diagnostics;
...
ProcessStartInfo proc = new ProcessStartInfo("open", "sh talk.sh \"Hello and stuff\"");
proc.UseShellExecute = false;
Process.Start(proc);
...

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.

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.

using UnityEngine;
using System.Collections;
using System.Diagnostics;

public class talk : MonoBehaviour {

	void Start () {	
		ProcessStartInfo proc = new ProcessStartInfo();
		proc.FileName = "open";
		proc.WorkingDirectory = "/users/myUserName";
		proc.Arguments = "talk.sh";
		proc.WindowStyle = ProcessWindowStyle.Minimized;
		proc.CreateNoWindow = true;
		Process.Start(proc);
		UnityEngine.Debug.Log("Halløjsa");
	}
	
	void Update () {
	
	}
}
1 Like

This should work without the need for a pre written script and does not open the terminal window

private IEnumerator Speak (string command){
   
        ProcessStartInfo startInfo = new ProcessStartInfo("/bin/bash");
        startInfo.WorkingDirectory = "/";
        startInfo.UseShellExecute = false;
        startInfo.RedirectStandardInput = true;
        startInfo.RedirectStandardOutput = true;

        Process process = new Process();
        process.StartInfo = startInfo;
        process.Start();

        process.StandardInput.WriteLine("say " + command);
        process.StandardInput.WriteLine("exit");  // if no exit then WaitForExit will lockup your program
        process.StandardInput.Flush();

        string line = process.StandardOutput.ReadLine();
       
        process.WaitForExit();
        yield return null;
    }
8 Likes

How can I change the above code to do something like a bash command “flac sample.wav” on my mac system?