Write command to terminal from unity mac

Hi,

Im trying to use Unity (cause Im more into javascript and I don’t know any other engine/framework using it) to send string command to my mac terminal so I could use the TTS say command.

I know I should use system.diagnostics but the only thing I manage to do is opening the terminal with the following line:

System.Diagnostics.Process.Start("open", "-a 'Terminal' --args --no-first-run");

I need to pass in argument (write) but I don’t find anything about it.

Using msdn doesn’t want to work so Im a bit lost.
Also, I didn’t try to make the code run after building it as a standalone but I don’t think it will change anything.

Does someone would tell me in what direction to look?

Thanks a lot for your help

i’ll was asking myself the same thing some weeks ago and ended up write a shell script that opens the terminal and passes a string from unity.
if put in username in the paths instead of my name

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

public class TextToSpeech 
{
	public static void SpeakText(string s)
	{
		string file = "/Users/username/Desktop/text.txt";
		File.WriteAllText(file, s);
		Process p = new Process();
		p.StartInfo.UseShellExecute = false ;
		p.StartInfo.RedirectStandardInput = true;
		p.StartInfo.RedirectStandardOutput = true;

		p.StartInfo.FileName ="open";
		p.StartInfo.Arguments = "Talk.command";
		p.StartInfo.WorkingDirectory="/Users/username/Desktop";
		p.StartInfo.CreateNoWindow = true;

		p.Start();
	}
}

which would be used as

using UnityEngine;
using System.Collections;

public class InputGUI : MonoBehaviour 
{

	public string textInput;

	void OnGUI () 
	{
		textInput = GUI.TextField(new Rect(Screen.width/2 - 150, Screen.height/2 - 50, 300, 100), textInput);
		if(GUI.Button(new Rect(Screen.width/2 + 50, Screen.height/2 + 60, 100, 25), "Say It"))
		{
			TextToSpeech.SpeakText(textInput);
		}
	}
}

the shell script (with .command extension) is simply

 #!/bin/bash

TEXT=`cat /Users/username/Desktop/text.txt`

say -v Vicki "$TEXT"

This should work without the need for an external script.

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");
        process.StandardInput.Flush();

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