January 22nd, 2009
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.
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.
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
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.
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.
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