How do I launch terminal commands into multiple tabs?

6,704

Solution 1

One option is to use System Events to send the keystroke necessary to create the new tab, but the limitation is that Assistive Devices in Universal Access must be turned on and adding a sight delay may be necessary.

tell application "System Events" to tell process "Terminal" to keystroke "t" using command down

As far as I can discern from the Terminal script dictionary - you can only retrieve information from tabs but not create new ones as you could with windows (eg. Run do script "clear"

Solution 2

Looks like you can use GUI scripting. (I'd have expected to be able to make new tabs without resorting to GUI scripting, since Terminal's dictionary knows tabs as elements of windows. Apparently broken, though.) As with Chealion's answer, access for assistive devices must be turned on but Apple provides a script that allows you to check that it's on here.

http://www.apple.com/applescript/uiscripting/

Matthew Lambie provides an example of using AppleScript's GUI scripting to create tabs in Terminal.app here:

http://lambie.org/2007/11/03/tabs-in-terminal-using-applescript-on-leopard/

This answer would have been better as a comment to Chealion's answer but I don't have the rep to make comments yet.

Solution 3

I created a scriptable way to allow one to create and title new tabs from the same Terminal.app window using a new_tab(title, command) shell function that basically wraps around all the apple scripting.

My article with the code and some backstory can be found here: Programmatically Create & Title Tabs Within The Mac OS X Terminal.app

I hope this makes someone's life a bit more automated :)

Solution 4

iTerm has good scripting support. AFP 548 has an example that opens multiple ssh sessions in different tabs. [I suspect this will be easier to do and more reliable than GUI scripting.]

Share:
6,704

Related videos on Youtube

James Fleming
Author by

James Fleming

Father, husband, son, grandson, brother, programmer, entrepreneur, photographer, DJ, chef, sailor, mountain biker.

Updated on September 17, 2022

Comments

  • James Fleming
    James Fleming almost 2 years

    Is there a way with Applescript to launch multiple tabs in Terminal and execute commands against them?

    I have a project that requires me to launch several services in Terminal to run and monitor simultaneously and would like to automate that start-up process.