Class: Growl::Notifier

Inherits:
OSX::NSObject
  • Object
show all
Defined in:
lib/growl.rb

Constant Summary collapse

VERSION =
'1.0.2'
GROWL_IS_READY =
"Lend Me Some Sugar; I Am Your Neighbor!"
GROWL_NOTIFICATION_CLICKED =
"GrowlClicked!"
GROWL_NOTIFICATION_TIMED_OUT =
"GrowlTimedOut!"
GROWL_KEY_CLICKED_CONTEXT =
"ClickedContext"
PRIORITIES =
{
  :emergency =>  2,
  :high      =>  1,
  :normal    =>  0,
  :moderate  => -1,
  :very_low  => -2,
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#always_callbackObject

Set to true if you want to receive delegate callback messages, growlNotifierClicked_context & growlNotifierTimedOut_context, without the need to specify a :click_context.

The default is false, which means your application won’t receive any delegate callback messages if the :click_context is omitted.



36
37
38
# File 'lib/growl.rb', line 36

def always_callback
  @always_callback
end

#application_iconObject (readonly)

Returns the value of attribute application_icon.



27
28
29
# File 'lib/growl.rb', line 27

def application_icon
  @application_icon
end

#application_nameObject (readonly)

Returns the value of attribute application_name.



27
28
29
# File 'lib/growl.rb', line 27

def application_name
  @application_name
end

#default_notificationsObject (readonly)

Returns the value of attribute default_notifications.



27
28
29
# File 'lib/growl.rb', line 27

def default_notifications
  @default_notifications
end

#delegateObject

Returns the value of attribute delegate.



28
29
30
# File 'lib/growl.rb', line 28

def delegate
  @delegate
end

#notificationsObject (readonly)

Returns the value of attribute notifications.



27
28
29
# File 'lib/growl.rb', line 27

def notifications
  @notifications
end

Class Method Details

.sharedInstanceObject

Returns the singleton instance of Growl::Notifier with which you register and send your Growl notifications.



22
23
24
# File 'lib/growl.rb', line 22

def sharedInstance
  @sharedInstance ||= alloc.init
end

Instance Method Details

#notify(notification_name, title, description, options = {}, &callback) ⇒ Object

Sends a Growl notification.

  • notification_name : the name of one of the notifcations that your apllication registered with Growl. See register for more info.

  • title : the title that should be used in the Growl notification.

  • description : the body of the Grow notification.

  • options : specifies a few optional options:

    • :sticky : indicates if the Grow notification should “stick” to the screen. Defaults to false.

    • :priority : sets the priority level of the Growl notification. Defaults to 0.

    • :click_context : a string describing the context of the notification. This is send back to the delegate so you can check what kind of notification it was. If omitted, no delegate messages will be send. You can disable this behaviour by setting always_callback to true.

    • :icon : specifies the icon to be used in the Growl notification. Defaults to the registered application_icon, see register for more info.

Simple example:

name = 'YourHamburgerIsReady'
title = 'Your hamburger is ready for consumption!'
description = 'Please pick it up at isle 4.'

Growl::Notifier.sharedInstance.notify(name, title, description)

Example with optional options:

Growl::Notifier.sharedInstance.notify(name, title, description, :sticky => true, :priority => 1, :icon => OSX::NSImage.imageNamed('SuperBigHamburger'))

When you pass notify a block, that block will be used as the callback handler if the Growl notification was clicked. Eg:

Growl::Notifier.sharedInstance.notify(name, title, description, :sticky => true) do
  user_clicked_notification_so_do_something!
end


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/growl.rb', line 86

def notify(notification_name, title, description, options = {}, &callback)
  dict = {
    :ApplicationName => @application_name,
    :ApplicationPID => pid,
    :NotificationName => notification_name,
    :NotificationTitle => title,
    :NotificationDescription => description,
    :NotificationPriority => PRIORITIES[options[:priority]] || options[:priority] || 0
  }
  dict[:NotificationIcon] = options[:icon].TIFFRepresentation if options[:icon]
  dict[:NotificationSticky] = 1 if options[:sticky]
  
  context = {}
  context[:user_click_context] = options[:click_context] if options[:click_context]
  if block_given?
    @callbacks[callback.object_id] = callback
    context[:callback_object_id] = callback.object_id.to_s
  end
  dict[:NotificationClickContext] = context if always_callback || !context.empty?
  
  notification_center.postNotificationName_object_userInfo_deliverImmediately(:GrowlNotification, nil, dict, true)
end

#onClicked(notification) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/growl.rb', line 113

def onClicked(notification)
  user_context = nil
  if context = notification.userInfo[GROWL_KEY_CLICKED_CONTEXT]
    user_context = context[:user_click_context]
    if callback_object_id = context[:callback_object_id]
      @callbacks.delete(callback_object_id.to_i).call
    end
  end
  
  @delegate.growlNotifierClicked_context(self, user_context) if @delegate && @delegate.respond_to?(:growlNotifierClicked_context)
end

#onReady(notification) ⇒ Object



109
110
111
# File 'lib/growl.rb', line 109

def onReady(notification)
  send_registration!
end

#onTimeout(notification) ⇒ Object



125
126
127
128
129
130
131
132
133
# File 'lib/growl.rb', line 125

def onTimeout(notification)
  user_context = nil
  if context = notification.userInfo[GROWL_KEY_CLICKED_CONTEXT]
    @callbacks.delete(context[:callback_object_id].to_i) if context[:callback_object_id]
    user_context = context[:user_click_context]
  end
  
  @delegate.growlNotifierTimedOut_context(self, user_context) if @delegate && @delegate.respond_to?(:growlNotifierTimedOut_context)
end

#register(application_name, notifications, default_notifications = nil, application_icon = nil) ⇒ Object

Registers the applications metadata and the notifications, that your application might send, to Growl. The default_notifications are notifications that will be enabled by default, the regular notifications are optional and should be enabled by the user in the Growl system preferences.

Register the applications name and the notifications that will be used.

  • default_notifications defaults to the regular notifications.

  • application_icon defaults to OSX::NSApplication.sharedApplication.applicationIconImage.

    Growl::Notifier.sharedInstance.register ‘FoodApp’, [‘YourHamburgerIsReady’, ‘OhSomeoneElseAteIt’]

Register the applications name, the notifications plus the default notifications that will be used and the icon that’s to be used in the Growl notifications.

Growl::Notifier.sharedInstance.register 'FoodApp', ['YourHamburgerIsReady', 'OhSomeoneElseAteIt'], ['DefaultNotification], OSX::NSImage.imageNamed('GreasyHamburger')


51
52
53
54
55
56
# File 'lib/growl.rb', line 51

def register(application_name, notifications, default_notifications = nil, application_icon = nil)
  @application_name, @application_icon = application_name, (application_icon || OSX::NSApplication.sharedApplication.applicationIconImage)
  @notifications, @default_notifications = notifications, (default_notifications || notifications)
  @callbacks = {}
  send_registration!
end