Class: Meow

Inherits:
Object
  • Object
show all
Defined in:
lib/meow.rb,
lib/meow/autotest.rb,
lib/meow/notifier.rb

Defined Under Namespace

Classes: Autotest, Notifier

Constant Summary collapse

VERSION =
'2.1.0'
PRIORITIES =
{  :very_low   => -2,
                :moderate   => -1,
                :normal     =>  0,
                :high       =>  1,
                :emergency  =>  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"
@@application =

This sets up shared state properly that Cocoa uses.

OSX::NSApplication.sharedApplication
@@callbacks =

Holds blocks waiting for clicks

Notifier.new
@@background_runner =

The thread that is responsible for catching native callbacks.

nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, note_type = 'Note', icon = OSX::NSWorkspace.sharedWorkspace.iconForFileType('rb')) ⇒ Meow

Create a new Meow object.

  • name is the application name.

  • note_type is the type of note you send.

  • icon is the icon displayed in the notification.

Example:

note = Meow.new('My Application')


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/meow.rb', line 72

def initialize(name, note_type = 'Note', icon = OSX::NSWorkspace.sharedWorkspace.iconForFileType('rb'))
  @name       = name
  @icon       = icon
  @note_type  = note_type
  @registered = []

  @pid = OSX::NSProcessInfo.processInfo.processIdentifier

  # The notification name to look for when someone clicks on a notify bubble
  # or the bubble is timed out.
  @clicked_name = "#{name}-#{@pid}-#{GROWL_NOTIFICATION_CLICKED}"
  @timeout_name = "#{name}-#{@pid}-#{GROWL_NOTIFICATION_TIMED_OUT}"

  notify_center = OSX::NSDistributedNotificationCenter.defaultCenter
  notify_center.objc_send(:addObserver, @@callbacks,
                          :selector, "clicked:",
                          :name, @clicked_name,
                          :object, nil)
  notify_center.objc_send(:addObserver, @@callbacks,
                          :selector, "timeout:",
                          :name, @timeout_name,
                          :object, nil)

  start_runner unless @@background_runner
end

Instance Attribute Details

#iconObject

Returns the value of attribute icon.



61
62
63
# File 'lib/meow.rb', line 61

def icon
  @icon
end

#nameObject

Returns the value of attribute name.



61
62
63
# File 'lib/meow.rb', line 61

def name
  @name
end

#note_typeObject

Returns the value of attribute note_type.



61
62
63
# File 'lib/meow.rb', line 61

def note_type
  @note_type
end

Class Method Details

.import_image(image, size = 128) ⇒ Object

Convert image to an NSImage that displays nicely in growl. If image is a String, it’s assumed to be the path to an image on disk and is loaded.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/meow.rb', line 42

def import_image(image, size=128)
  if image.kind_of? String
    image = OSX::NSImage.alloc.initWithContentsOfFile image
  end

  return image if image.size.width.to_i == 128

  new_image = OSX::NSImage.alloc.initWithSize(OSX.NSMakeSize(size, size))
  new_image.lockFocus
  image.drawInRect_fromRect_operation_fraction(
    OSX.NSMakeRect(0, 0, size, size),
    OSX.NSMakeRect(0, 0, image.size.width, image.size.height),
    OSX::NSCompositeSourceOver, 1.0)
  new_image.unlockFocus

  return new_image
end

.notify(name, title, description, opts = {}) ⇒ Object

Send a message in one call.

Example:

Meow.notify('Meow', 'Title', 'Description', :priority => :very_high)


34
35
36
# File 'lib/meow.rb', line 34

def notify(name, title, description, opts = {})
  new(name).notify(title, description, opts)
end

Instance Method Details

#notify(title, description, opts = {}, &block) ⇒ Object

Send a notification to growl.

  • title will be the title of the message.

  • description is the description of the message

  • opts is a hash of options.

  • block is an optional block passed to notify. The block is called when someone clicks on the growl bubble.

Possible values for opts are:

  • :priority => Set the note priority

  • :icon => Override the current icon

  • :note_type => Override the current note type

See Meow::PRIORITIES for the possible priorities.

Example:

note.notify('title', 'description', :priority => :very_low)


116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/meow.rb', line 116

def notify(title, description, opts = {}, &block)
  opts = {
    :icon       => icon,
    :sticky     => false,
    :note_type  => note_type,
    :priority   => 0
  }.merge(opts)

  register(opts[:note_type]) unless @registered.include?(opts[:note_type])
  opts[:icon] = Meow.import_image(opts[:icon]) if opts[:icon].is_a?(String)

  notification = {
    :ApplicationName   => name,
    :ApplicationPID    => @pid,
    :NotificationName  => opts[:note_type],
    :NotificationTitle => title,
    :NotificationDescription => description,
    :NotificationIcon  => opts[:icon].TIFFRepresentation(),
    :NotificationPriority => opts[:priority].to_i
  }

  notification[:NotificationAppIcon] = opts[:app_icon].TIFFRepresentation if opts[:app_icon]
  notification[:NotificationSticky] = OSX::NSNumber.numberWithBool(true) if opts[:sticky]

  notify_center = OSX::NSDistributedNotificationCenter.defaultCenter

  if block
    notification[:NotificationClickContext] = @@callbacks.add(block)
  end

  notify_center.postNotificationName_object_userInfo_deliverImmediately('GrowlNotification', nil, notification, true)
end