Old school Dumb Terminal image.Via a post on OneThingWell, I discovered a new Ruby library and accompanying CLI called terminal-notifier. It allows you to quickly interface with Mountain Lion’s Notification Center from the command line. terminal-notifier will be available as part of the filesystem-watching tool Kicker, but you can put it to use as a general notification system for Terminal right now.

The script below is just a quick example that I’m using to ping me after a longer-running script finishes, similar to what I was doing with growlnotify. It does nothing that the terminal-notify CLI doesn’t already do, it just wraps the needed syntax in a single command I can run quickly in sequence with other commands when working at the prompt.

To run, you need the terminal-notifier gem installed. You can do so with the command:

gem install terminal-notifier

You may need to run sudo gem install terminal-notifier if it gives you permission errors.

# finished: a quick script to notify you when a command is done using Mountain Lion notifications
# It grabs the current folder as the title and takes one argument as the message
# Example: make && make install && finished "Done compiling" || finished "compiler failed"
# Needs the terminal-notifier gem : `gem install terminal-notifier`
# Can alternately be used with the CLI <https://github.com/alloy/terminal-notifier>
# (remove require block below and swap comment lines at the bottom)

require 'rubygems'
require 'terminal-notifier'

message = ARGV[0] || "Task completed"
project = Dir.pwd.split('/')[-3..-1].join('/')
activate = 'com.googlecode.iterm2'

TerminalNotifier.notify(message, :activate => activate, :title => "Finished in #{project}")
# %x{/usr/bin/terminal-notifier -message "#{message}" -title "Finished in #{project}" -activate #{activate}}

Save it as a file called finished and drop it in a folder in your path. Use /usr/local/bin if nothing else. You can also put it in a scripts folder outside of your path (and optionally name it something else), then create a finished alias that calls it with a full path.

You can change or remove the activate variable and associated call; that’s what will open when you click a notification. If you use iTerm2, it’s good to go. Unless you run in Visor mode with a hotkey, then it’s less than useful (I found out). If you use another terminal or want to open something else entirely, put its bundle identifier in there.

You can also add an open option to the call which, instead of activating an application will open a URL scheme instead. Depending on your project, that could be useful. I’m thinking about building an external URL handler for iTerm2 that uses AppleScript to open open specific session ids and terminals, but haven’t gotten there yet.

Example usage for finished:

make && make install && finished "Done compiling" || finished "Compiler failed"

The || in only useful with commands that return 0 on success and non-0 on fail. A lot of basic commands will just return fail. If you just want to know when it’s done, fail or no fail, use:

find . -name "brett*"; finished

That will use a default message and you’ll get whatever notification you assign in Notification Center preferences. Have fun.