No, it’s not about Transformers :)
Spark is something that is not needed in normal operating systems, that use customizable window managers — a keyboard shortcuts manager. That is, it will sit in background, intercept keyboard events, and do stuff. What kind of stuff? Well, it depends on how you configure it. It can run arbitrary commands, Applescripts, open documents, change system settings — nothing too fancy, but it has one major advantage, it works flawlessly, without trying to impose any extra philosophy on you.¹ You press something on your keyboard, and stuff happens. Simple, eh?
Around the same time I’ve discovered Spark, I’ve also decided to ditch iTerm. While generally awesome, it has one major disadvantage — it’s slow. It got way better over time, from completely unusable, to quite usable. Problem is, I end up with large terminals. And that’s still the moment when iTerm hiccups, which results in 1,5-3s delay for screen redraws, be it screen switching, or Vim redrawing its window. Terminal.app, this blank and uninteresting application that came together with the system didn’t have any problems like that, so I decided I’ll give it a try.
Basic set of customizations was easy to do. Proper colors (white-on-black instead of default eye-burning black-on-white) and proper font. Being used to iTerm, I wanted Terminal.app to mimic it at least a bit. First goal: make Cmd+Left/Right switch tabs. That was relatively easy — I’ve asked Spark to make a application-specific keybinding (one that will work only in Terminal.app) to click a menu item for me (Window → Select Previous Tab). Yes, that was way too easy.
Next goal was to have specific profiles of terminal bound to keyboard shortcuts. That is, ability to press Cmd+h for a new tab, with ssh host1 launched inside, Cmd+p for a new tab with ssh host2… you get the idea. And here’s where problems started. The requested action for such keybind was to:
- open a new tab
- exec ssh host
Sounds Applescript-ish, right? I’ve opened script editor, pointed it at Terminal… only to discover that there’s no Applescript interface for ‘open a new tab’. Apparently, tabs are kind of fancy addition in OSX 10.5, and users should be happy that those made it in. No whining. Allright, one Google search later I found a twisted way of telling Terminal that it should launch a new tab — simulate a keystroke (Cmd-t). Good. One extra line and we’re set. Script worked from both command line and Spark config window. There.
…but it failed to work when bound to a key. What the…? I’ve poked around, asked on the forum, and it turned out that simulating a keystroke in a script that is used in Spark is not a good idea. Sigh. Next attempt was to simulate keystrokes with Spark. Just: cmd+t,e,x,e… Yeah, that worked. Assuming I’ve set the delay between key presses to something more than 5ms. If it was lower, some keypresses were getting lost. So allright, 6ms delay, and it works. I press the key, new tab launches… and hey, just like in a cheap hacker movie, characters slowly appear…
Here’s a bit of Applescript I’ve finally ended up with next day:
tell application "Terminal" to activate tell application "System Events" click menu item ¬ "default" in menu "New Tab" of menu item ¬ "New Tab" in menu "Shell" of menu bar item ¬ "Shell" in menu bar 1 of process "Terminal" end tell tell application "Terminal" to do script "clear; exec ssh myawesome.host" in first window
default is the name of Terminal.app setting (the one from Settings tab of Terminal’s preferences). Last line script (“clear; exec ssh myawesome.host“) is simply the set of commands to execute in newly created tab.
¹ Hey, I like Quicksilver. I really do. I just don’t use it, because I don’t really find a need for its fancy features.