Terminal.app window tab groups

2012 04 13

Here’s a fun one: if you ever find yourself setting up a Terminal.app window with the same set of tabs, running the same set of (let’s say) daemons over and over again, you can try out this little helper script.

#!/bin/bash

TOTAL=$#
while (( "$#" )) ; do
	let I=$TOTAL-$#
	if [ $I -eq 0 ] ; then
		# first command: need a new window
		osascript -e "tell application \"Terminal\" to \
		  do script" >/dev/null
	else
		# subsequent commands: new tab in that window
		osascript -e "tell application \"System Events\" to \
		  tell process \"Terminal\" to \
		    keystroke \"t\" using command down" >/dev/null
	fi
	osascript -e "tell application \"Terminal\" to \
	  do script \"$1\" in selected tab of the front window" >/dev/null
	shift
done

Use it by writing one script per window-tab-group you want to be able to easily spawn.

#!/bin/sh

terminal-tab-group.bash \
    "first-daemon" \
    "cd /some/path && ./second-daemon -f -l -a -g -s" \
    "/usr/sbin/third-daemon" \