Programmatically launch Terminal.app with a specified command (and custom colors)

dreeves picture dreeves · Dec 10, 2010 · Viewed 34.9k times · Source

I can launch an xterm from the command line (or a program, via a system call) like so:

/usr/X11/bin/xterm -fg SkyBlue -bg black -e myscript

That will launch an xterm with blue text and a black background, and run an arbitrary script inside it.

My question: How do I do the equivalent with Terminal.app?

Answer

Sam Deane picture Sam Deane · Oct 19, 2011

You can open an app by bundle id too, and give other parameters.

If there's an executable script test.sh in the current directory, the following command will open and run it in Terminal.app

 open -b com.apple.terminal test.sh 

The only down side that I can find is that Terminal doesn't appear to inherit your current environment, so you'll have to arrange another way to pass parameters through to the script that you want to run. I guess building the script on the fly to embed the parameters would be one approach (taking into account the security implications of course...)