Falling Leaves: the Ardes blog
Nick Rutherford

Necessity may well be the mother of all creation but I have oft found laziness and irritation to be close relations. As a new user I'm still settling into a Mac and Rails working environment. I'm using TextMate and Terminal.app with several tabs in the same window.

I soon grew tired of tapping this kind of sequence into my macbook:

gem server
⌘T
cd dev/workspace/projectname
script/server
⌘T
cd dev/workspace/projectname
script/autospec
⌘T
cd dev/workspace/projectname
mate .

It seemed sensible that there would be some way to script this. In the end Applescript turned out to be the way forward - the problem was too osx-centric to do with a bash script in any way I could think of or find.

The Solution

I came across "Open Terminal Here" that did almost what I wanted, and hacked it into what I wanted: a new finder toolbar button which when clicked will pop open a terminal window then populate it with tabs and running processes. Download from here, extract to somewhere you're happy with it living, drag it into your finder toolbar. There should also be support for dragging directories onto it or piping things into it, ymmv.

How it works

This is achieved by using applescript to fake keyboard input to Terminal.app. This makes a change from typical API work! Unfortunately it can make it a tad quirky and unreliable too. I have had several processes try and execute in the same tab before. Nothing exploded, but it isn't what should happen.

Modification

The bits of interest for customising it to your needs are found as follows:

  • right click the app and 'show package contents'
  • open Contents/Scripts/main.scpt (Apple's Script Editor is good for this, though other text editors may be suitable too)
  • modify on "activate_terminal" to contain the processes you desire. launch_tab will create a new terminal tab and run the command you provide as a parameter string.

For me this looks like:

on activate_terminal(the_path)
    tell application "Terminal"
        do script with command "gem server"
        tell application "Terminal" to activate
        my launch_tab("cd " & quoted form of the_path & " && script/server")
        my launch_tab("cd " & quoted form of the_path & " && script/autospec")
        my launch_tab("cd " & quoted form of the_path & " && mate .")
    end tell
end activate_terminal

Where

on launch_tab(with_command)
    tell application "System Events" to tell process "Terminal" to keystroke "t" using command down
    tell application "Terminal" to do script with command with_command in window 0
end launch_tab

Improvement comments are welcome, Applescript is something I am new to.

If this isn't quite what you want do check out the origin, others have been playing around with this stuff too and the site was updated after I produced this.

Complimenting Scripts

In addition to the vanilla "Open Terminal Here" app the lselect app is very useful - it selects things in your current finder window by globs (the patterns you might feed to rm or ls in a shell, simple string patterns). These have been re-iconified too, making them fit in seamlessly with Apple's Leopard buttons.

Alternative

For an alternative way to do this, starting from within your Textmate project and using GNU screen (rather than Terminal.app tabs) see Joel Chippindale's work.

To see a partial recreation of this as a ruby script see my other post

2 Responses to “Rails Workbench (multi-tab terminal launcher)”

  1. Joel Chippindale

    Joel Chippindale Says:

    Very neat. I did something very similar with with a Textmate screen plugin that enables you to set up per project screen configurations so that script/server, script/console etc. are started in separate windows.
  2. Nick Rutherford

    Nick Rutherford Says:

    Screen is a nice alternative, certainly. I was using this for a while in OS X but dropped it in the end - having tabs provided with the OS meant I could remove a dependency from my workcycle (if that makes any sense).

    Configuring screen initially was a bit tricky but I had help from a Linux using friend who is really into this stuff. Session saving was something in particular that I liked, but it's not really necessary with autospec and mongrel. I've often had trouble with unix apps in osx in general, missing keys, configuration files and so on. The tools are great, but time consuming for new users such as myself. It's a shame Apple doesn't support them more actively, but I suppose too few people would use them for it to be worthwhile.

    Your option for having different different commands for different projects is a nice addition. Unfortunately Apple haven't supported Terminal.app very well yet - I wanted to improve this following a switch to rubyosa but it seems it's not that simple.
    There's probably a way to fudge some ruby into producing some applescript dynamically (from a project file) then running it.
    Regards
    Nick