I need to start a process in a new console window. For macs, I found something like this: Running a command in a new Mac OS X Terminal window where the command to run is passed as a string.
So I made a method that given a list of Strings (like process builder), returns the list of Strings (the final command) that will run the given command in a new console window. I could append the given strings with spaces, which would work most of the time, but how would I escape it properly? For example, I can pass the process builder new String[] { "echo", "hello world" }
and it actually does echo "hello world"
. And it deals with a lot of other cases too (I think).
I think the explanation is complicated so here is a pseudo-stub:
public static String[] getConsoleCommand(String[] command) {
if operating system is Mac...
String commandString = concatenate command...
return new String[] { "osascript", "-e",
String.format("'tell application \"Terminal\" to do script \"%s\"'",
commandString.replace("'", "\\\'")) // escape single quote used in 'tell application...'
};
}