Scripting Terminal.app to setup my Sakai workspace

With my software development I require a few Terminal windows or tabs open at any one time. Normally at least 4. When Terminal.app came out with tabbed Terminal, I quickly realised that ‘Save Windows as Group’ doesn’t really do much.

I mean, great, I can setup my tabs and save them so it will automatically give me 4 tabs in a window. But I could just hit command-T a few times too. What I really wanted was the ability to save the state of each of those tabs, ie which directory I am in, which command is currently running etc.

So I came up with some AppleScript a while back for doing exactly what I want, and now on the odd occasion that I need to restart my Mac, I just run this Applescript to setup my Terminal workspace rather than doing it all manually each time.

(Open terminal and load up tabs with the specified commands, Steve Swinsburg, November 2008)

set cols to 160
set rows to 30
set commands to {"cd ~/dev/eclipse/profile2/", "cd ~/dev/sakai/src/cafe-2.5.x/profile2/", "cd ~/dev/sakai/init.d/", "cd ~/dev/sakai/tomcat/cafe-2.5.x/logs && tail -f catalina.out"}
repeat with command in commands
  new_tab(command)
end repeat

on new_tab(command)
  tell application "Terminal"
    activate
    tell application "System Events" to tell process "Terminal" to keystroke "t" using command down
    do script command in selected tab of the front window
  end tell
end new_tab

Just open Applications/AppleScript/Script Editor.app, paste that in, change the actual commands to what you want, add and delete command as necessary (depending on how many Terminal tabs you want), save and run! You can even save it as an Application with startup screen to confirm that you really want to run it etc.

I’ve got one for my current development, one for my Sakai branch management workspace, can be applied to anything. I *really* love that you can run the command inside the Terminal tab when it opens.

Note to Apple: Implement this feature in ‘Save Windows as Group’!

Advertisements

Using a HashMap with a DropDownChoice component in Wicket

Lately, I’ve been mucking around with the DropDownChoice component in Wicket, mainly because I want to have separate id and display values in a dropdown choice. ie I want to render a label, eg “one”, “two”, “three”, but have Wicket see that as some id, eg 1,2,3. Simple stuff really.

At first glance I can see a HashMap being given in the constructor for a dropdown choice to be a really easy way to do this. Nope. All the documents and examples given seem to be for single dimensioned lists where the id and display values would be the same.

So after much frustration, I present the solution.

Firstly, wrap the HashMap with a Model and in its getObject method, return the keys of the HashMap (ie a List!). Then, in the DDC constructor, give it the model and use an IChoiceRenderer which will separate out the id from the display values and use them in the right spot.

final LinkedHashMap preferences = new LinkedHashMap();
preferences.put("0", "some choice");
preferences.put("1", "some other choice");
preferences.put("2", "and another choice");

IModel dropDownModel = new Model() {
  public Object getObject() {
    return new ArrayList(preferences.keySet());
  }
};

DropDownChoice profileChoice = new DropDownChoice("preference", dropDownModel, new IChoiceRenderer() {
  public String getDisplayValue(Object object) {
    return preferences.get(object);
  }
  public String getIdValue(Object object, int index) {
    return object.toString();
  }
});

Note that in this example I haven’t given my DDC a Model for the user selected data to be stored in. This is because my wrapping form has a CompoundPropertyModel (a POJO) attached to it and the id of my DDC (‘preference’) maps to a field in that Model.

Also, to set a default value for the DDC, just set a value for the property in the model.

In this case:

preferencesModel.setPreference(2);

will cause the DDC that is rendered to have the item that maps to ‘2’ selected already.

Not entirely Sakai related, but its code for a Sakai tool 🙂