Grails script notifications via Growl
Now that Grails 0.5 is out, I want to share how I use Growl on OS X to tell me when scripting tasks have completed.
This is very useful for me as I multitask heavily and will often leave a grails run-app or test-app running in a background terminal window. Using Growl – which provided pretty popup notification infrastructure for any application – I get a nice message in big text sliding up the bottom of the screen to tell me when Grails is ready for me to get back to it.
First, you need to install Growl, and then install the growlnotify shell command which can be found in the DMG, under Extras/growlnotify. I found I had to copy the files from there onto my HD and run install.sh as superuser. I didn’t worry about messages relating to the man page.
Test this by running this in a command window:
You should see a popup Growl message. If not, check your PATH etc.
Next, you need to copy the following code and save it in ~/.grails/scripts/Events.groovy :
// StatusFinal event is triggered with the last message
// supplied by a script target.
eventStatusFinal = { msg ->
growlNotify(msg)
}
// StatusUpdate – every status message that is non-final
eventStatusUpdate = { msg ->
// I don‘t need all these so I don’t use growl here
// growlNotify(msg)
}
// Do the business with growlnotify
void growlNotify(String message) {
Ant.exec(executable:‘growlnotify’) {
arg(value:‘-n’)
arg(value:‘Grails’)
arg(value:‘-m’)
arg(value:message)
arg(value:‘Grails’)
}
}
Once you have this in place, go to an existing Grails project that has been upgraded to Grails 0.5 and do grails-run-app or similar. You can go to the Growl preferences pane and change the message style for just the Grails application also. I have mine set to the Music Video style and it looks something like this:
OK so you won’t get the Grails logo there, you’ll get a terminal icon. We don’t have a decent icon ready yet, but that will come in the future. To specify an image you just need to add a "–image filename" argument pair to the growlNotify function, and have a suitable image.
Current limitation – I can’t get growlnotify to generate different categories of Growl events, so you cannot customize the messages in a fine-grained way, you can only customize the look of all of them. If growlnotify can do this / adds this in future then we could have different display styles, times and priorities for these messages depending on the type of event that triggered them.
Other interesting possibilities:
- Change the script to send growl notifications to another machine on your network. This would enable you to get notifications from a remote test server
- Move this code into a GrowlNotificationPlugin for Grails, which can then be trivially installed in any project. Plugins can hook into events too by putting Events.groovy in <plugin>/scripts/
The full documentation and list of available events is here. Beyond notifications, scripting events promise to enable a whole load of interesting features in future including automatic addition of new created artefacts to SVN/CVS, and plugins that modify their behaviour based on other plugins being added.
No related posts.
Awesome! Can’t wait to try it out!
cheers
Whoops…hit send in mid-though. The above post should read:
Very nice tip. Instead of invoking ant to execute a process you can also use the following:
void growlNotify(String message) {
Thread.start{def p = Runtime.runtime.exec(“growlnotify -m ‘${s}’ Grails”);p.waitFor();p.destroy()}
}
Check out JGrowl [1] from Stephen Martin. It is Java code that can send growl notifications via the network. It also provides a basic Java version of Growl so that non-Mac users can get some Growl goodness.
Also, I’ve created an Ant BuildListener [2] that uses JGrowl to send Growl notifications when a build starts/ends.
[1] – http://homepage.mac.com/stevevm/JGrowl/JGrowl.tar.gz
[2] – http://code.google.com/p/growlbuildlistener/