Growl - Best of Windows World on Mac
Posted 11/15/2008 - 13:18 by cocoacast
When I first saw what Growl does a few years back, my first thought was “Oh my God, they moving Windows API to Mac.” As you can imagine, that is a horror thought to have. If you had never seen Growl in action and do not have a Mac, the best way to view what Growl does is to think of all those message bubbles that Windows is famous for;such as software update messages or messages notifying you that your neighbors APC was unplugged. The point I’m trying to make is that first impressions are wrong sometimes and I was wrong about Growl too. What Growl proves is the fact that even the worst technology that Microsoft can come up with, could be adopted on the Mac, and really make the idea shine.
It’s been a while since I first looked at Growl. My fellow podcaster, Philippe Guitard, made me do that by producing a show for CocoaCast that explained how to take advantage for Growl. You can view the show here, and so I would not go into too much detail about it here - just enough to get you interested and appreciate Growl for what it is.
The idea behind the Growl is really simple. There are applications on Mac that quietly do their job (small or big) in background either because the job is small or because the user hid the application. Suppose that the job is done and the application wants to bring attention of the user or just notify him or her about completion of the task. Prior to Growl the main way to do so would be some kind or sound notification or bouncing of the icon in the Dock. Well, user can turn off the sound and most users I know how to disable bouncing icons in the Dock. Well, in these cases you are out of luck and the user would not know that your application desperately wants your attention..
What Growl allows you to do is to register for a service running in background and send it messages. Once such message is received, Growl takes care of notifying the user by the means that are user configurable. Since Growl can easily be configured through System Preferences, it can be configured so the user would see a pop up bubble, hear sound, etc. with theme-able user interface. As you can see it is really simple and transparent to users. How about developers?
Growl from Developer Perspective
When you start to think about it, the solution to the notifications issue is really to have a background process that is running constantly and waiting for messages to be sent to it. This is exactly what Growl does. A Growl user simply downloads Growl and installs it in a system preference panel. Growl listens to notifications sent from various applications (the growing list is available on the Growl’s website) that depend on it. However, if you actually a developer who wants to send a notification to Growl there is a little bit more work you have to do. Growl comes as framework that you download and incorporate into your project. Again, the best way to see how it is done is to watch the show that Philippe Guitard created for CocoaCast podcast. The steps for using the framework are very logical. First, you have to register your message list with Growl service in order to send notifications. Once that is done, you would register your class (actually an object of your class) and a selector with Growl for receiving the callbacks from the Growl notification system. Finally, the only logical thing left to do is send a message to Growl and it would take care of the rest. As I mentioned, you have the opportunity to react to various external events, such as when the message was dismissed, ignored, etc.
No review of Growl would be complete from the developer perspective without presenting some code examples. So lets walk through the process of setting up the Growl and using it in your application.
Growl sources are provided as a framework and you have to download it from the Growl website. Once downloaded, use Xcode to add the framework to active target. Show that Philippe Guitard created for cocoacast.com shows this step in details.
Once you set up the framework, it is time to inform Growl about the types of messages you would be sending. You have to create a dictionary of messages and register them with Growl. Also, note that you can specify the delegate for Growl to call if/when messages are dismissed, disappeared, etc.
The following code declares that your class implements Growl protocol used for registration:
The following line registers with Growl Application Bridge:
Tell Growl about the messages you are going to sent it:
// For this application, only one notification is registered
NSArray* defaultNotifications = [NSArray arrayWithObjects:growlTestNotification, nil];
NSArray* allNotifications = [NSArray arrayWithObjects:growlTestNotification, nil];
NSDictionary* growlRegistration = [NSDictionary dictionaryWithObjectsAndKeys:
allNotifications, GROWL_NOTIFICATIONS_ALL, nil];
All set for sending the messages, so all that is left to do now is send a notification to shared Growl notification center object and Growl will take care of the rest.
// If the clickContext check box is checked in the UI, the current date/time NSDate will be passed...
[GrowlApplicationBridge notifyWithTitle:[notifyWithTitle stringValue]
iconData:[[iconData image] TIFFRepresentation]
isSticky:[isSticky state] == NSOnState ? YES : NO
clickContext:[clickContextButton state] == NSOnState ? [NSDate date] : nil];
Finally, you should really take a look at GrowlApplicationBridge.h file (also available in the source code you downloaded as part of the framework) for a lot of more things that Growl can do and the ways to communication with the framework.
As you saw from the code and discussion, Growl is a very elegantly designed framework. I really liked the ability of Growl to bring the best of Windows world to the Mac OS platform with Mac-like elegance and simplicity. It is really simple to use and you should take advantage of Growl in your applications. However, the story would not be complete if I did not add a work of caution. It is really easy to go over board with this framework and before you know it, you can flood the screen with messages that are annoying to users and defeat the whole purpose of Growl. So, my world of caution is that you have to use your judgement and ask your self the following question before using Growl: “If I were a user, do I absolutely have to know about this event?” I would only send Growl notifications when an absolute must is the answer.