Mac Tips & Tricks #8: Changing application icons

There are many reasons why you might want to change an application’s icon. In my case, I wanted to be able to distinguish the stable IntelliJ IDEA 11 version from an early access 11.1 build:

Here’s how I changed the default (blue) icon to the new (pink) one:

First, reveal the application’s installation location in Finder (from the Dock context menu: Options > Show in Finder). From within Finder, invoke the context menu on the selected application, and Show Package Contents. Locate the icon file (*.icns) inside Contents > Resources. Make a backup copy, edit the icon in your favorite image editor (I simply overlaid a red color filter in Acorn), and save it as a *.png.

Now comes the tricky part: Apple’s icon format includes more than just an image, it actually contains a collection of images in different sizes, resolutions & states. To create a new *.icns file, launch Icon Composer (which ships with OSX). Open the original application icon to check which sizes your new icon should support:
Icon Composer

Create a new file, drag & drop your edited *.png to the required image size containers, and save the result. Re-launch your application — tada!

IntelliJ IDEA: Reveal in Terminal

IntelliJ IDEA ships with a very handy “Reveal in Finder” action, but unfortunately, I am still a terminal person. Today I put together a tiny AppleScript which creates a new iTerm2 session and changes to a directory of choice:

on run dir
  tell application "iTerm"
    activate
    tell last terminal
      set mysession to (make new session at the beginning of sessions)
      tell mysession
        set name to "Default"
        exec command "/bin/bash -l"
      end tell
      tell mysession
        write text "cd " & dir
      end tell
    end tell
  end tell
end run

IDEA allows to add custom actions via Settings > External Tools. Save the above script to a file (e.g. launchTerminalAndChangeDir.scpt) and configure a new “Reveal in Terminal” action as follows:

Intellij IDEA: Reveal in Terminal

Additionally, you may want to associate a key binding via Settings > Keymap. I chose Ctrl-Shift-T, which seems intuitive and is not conflicting with any OSX defaults.

The Setup

I started my new job at edorasware last week, with a new & shiny 15-inch MacBook Pro. A fresh computer with a blank OS is both, a blessing and a curse — it takes quite a while until it feels like home again. And even after a week, I still miss a few configuration settings or shortcuts here and there.

Setting up the new machine was certainly an opportunity to consolidate the apps and tools I like and need. Inspired by The Setup, here’s what I currently use to get things done:

As a Java developer, I spend most of my time in IntelliJ IDEA, the best IDE ever (weight this statement by the fact that I used Eclipse for 7+ years before). My second home is the command line. I use iTerm2, which has excellent search support, provides mouseless copying, and is more unixy than the official Terminal. I’m still learning to use Finder efficiently. The TotalFinder plug-in, with tab support and its Folders on Top mode, is my latest attempt at making friends.

A few crucial things help me organize & navigate my workspace: I use three virtual desktops (via Mission Control) which I access by keyboard shortcuts — 1 for Mail/Calendar, 2 for Browser, and 3 for IDEA. A launcher helps me kick-start applications. I switched from Quicksilver to Alfred — it provides the same features and is under active development. Divvy tames all window sizes & positions, clearly a missing feature in OSX.

Transitioning from Snow Leopard to Lion was more challenging than anticipated, mainly because of its different mouse usage paradigms. Most of the built-in gestures are easy to use, but some of them collide with my old habits (how the heck can I turn off horizontal scrolling in order to re-map the swipe gesture to back/forward navigation in IDEA?!). A while back I forced myself to exclusively use taps (rather than clicks) on the trackpad. Wow — the absence of the mechanical noise makes such a difference, everything suddenly feels very light and smooth!

These are pretty much the apps at the core. In addition, I like Chrome, 1Password, TextMate, Dropbox, Skype, Spotify, Acorn, Inkscape, Lightroom and OmniGraffle — just to name a few.

What are your favorite tools we should all know about?

GAE + Gson + AngularJS

I have been playing with AngularJS lately. It is a JavaScript library with the following tag line:

What HTML would have been had it been designed for web apps.

All I can say after a few hours: HTML would haven been great! It would have had a clear MVC separation, data binding, dependency-injection, and it would have kept development simple & fun.

My web app know-how is very rusty, so I hacked together a simple proof-of-concept, just to see how AngularJS actually feels. Here are the crucial snippets from my Google App Engine Java back-end (to generate JSON), and the HTML/AngularJS view (to render it).

The Servlet part:

protected void doGet(...) {
  response.setContentType("text/plain");
  String[] movies = new String[]{
    "Buffalo 66",
    "Jackie Brown",
    "Vicky Cristina Barcelona"};
  response.getWriter().print(new Gson().toJson(movies));
}

The AngularJS Controller (controller.js):

function MovieListCtrl($xhr) {
  var self = this;
  $xhr('GET', 'http://localhost:8080/json',
    function(code, response) {
      self.movies = response;
    }
  );
}

The HTML/AngularJS View (index.html):

<body ng:controller="MovieListCtrl">
  Rahel's favorite movies:
  <ul>
    <li ng:repeat="movie in movies">
      {{movie}}
    </li>
  </ul>
</body>

Check out the official AngularJS Tutorial for more details and a ton of cool examples.

Scala Gems #4: Generating Data Matrices

Generating matrix-shaped test data is easy in any spreadsheet application: Enter a few cell values and use the fill handle to complete all others. But firing up a spreadsheet somehow feels lame. This Scala three-liner can do the same:

println(Array.fill(7, 3)("X").map(
  _.mkString("\t")).mkString("\n")
)

Is it readable? No. Is it handy? Yes!

Scala Gems #3: Named Parameters

Just a quick one this time (it’s easter holiday and the weather is simply too beautiful to waste time in front of a computer). Anyway, have a look at this constructor call:

MigrosEgg(GREEN, BLUE)

Unless you’re Swiss and just know that I’m referring to the “Extra” variant, wouldn’t it be a lot less ambiguous to write

val aMigrosEgg = MigrosEgg(dotColor = GREEN, bgColor = BLUE)

As of Scala 2.8 you can name your arguments. You can also leave out those which have default values, shuffle their order — or you can simply have another one of those little suckers:

val notAMigrosEgg = aMigrosEgg.copy(dotColor = PINK)

Happy easter everyone!

Scala Gems #2: Getting Functional

As promised, here is the second post in my haphazardly thrown together Scala series. Over the past few days I have read some chapters in Odersky’s Programming in Scala, and most importantly, I have written my first ~100 lines of code (a basic Minesweeper app with a simple Swing UI). It’s been a lot of fun, and I am still very enthusiastic about many new concepts & constructs. When coding, the biggest challenge is to not fall back to imperative Java style, but really use the functional concepts wherever applicable. I am probably still not radical enough, but for the time being, here is my take on revealing non-mined cells:

def reveal = {
  if (revealed == false) {
    revealed = true
    if (adjacentMineCount == 0) {
      adjacentCells.foreach (cell => cell.reveal)
    }
  }
}

In summary: 1 usage of => (and even a recursion ;-) ).

Scala Gems: #1

This article finally did it: “Guardian.co.uk Switching from Java to Scala” — it made me jump head first into the Scala newbie pool. Of course I wanted to learn Scala for quite some time already, but I somehow never managed to get my act together.

Life as a Java programmer had gotten harder over the last few years. Not that I don’t enjoy my job anymore, to the contrary, but reading about all these highly dynamic youngsters hacking together their Python/Ruby/Groovy wizardy at the speed of light has made me feel even older than I already am. Old-fashionedness at its finest: I absolutely can’t relate to all this enthusiasm for dynamic languages. Ok, I admit that some script-fu will certainly help getting this nasty little problem solved quickly, but how the heck will a huge Rails project be maintainable over 10+ years? Or do we still have to wait longer for time to tell that it will fail? Do these people write tests for each and every thing that otherwise would be checked by a compiler? Isn’t this a rather high price for a little duck-typing?

I am obviously being cynical, but luckily I am not the only one in doubt. Some great brains also think that throwing away the type system is not the best compromise to make in order to get rid of Java’s (admittedly extensive) boilerplate. In this excellent talk, Bill Venners speaks about his preference for typed languages. He mentions “deterministic refactoring” as the main benefit, which he explains like this:

I don’t like types because I think they proof my program correct [...] but because they can proof changes that I make to my programs are correct

Coming back to Scala: It seems to combine the best of all worlds — strictly typed, functional & object-oriented, radically concise, and just close enough to the Java world for old-timers to transition gradually ;-)

To keep my new Scala heart beating, I will start posting weekly Scala gems. Let me start with the example given in the above interview — just to illustrate what “no boilerplate” and “concise” really mean:

Java:

public class HowdyClass {

    private String name;

    public HowdyClass(String name) {
        this.name = name;
    }

    public String sayHowdy() {
        return "Howdy " + name;
    }
}

and in Scala:

class HowdyClass(name: String) {
    def sayHowdy = "Howdy " + name
}

The second example is my personal highlight after roughly two hours of playing around with the interpreter: Initializing a two-dimensional grid with indexed cells:

class Cell(x: Int, y: Int)
Seq.tabulate(3, 9)(new Cell(_, _))

This is actually almost too concise (or at least as a functional noob it took me quite some time to understand it). Here’s a verbose equivalent which is a bit more self explanatory:

Seq.tabulate(3, 9)((x: Int, y: Int) => new Cell(x, y))

And now go get it and have a lot of fun!