Class: GrowlNotifyOsascript

Inherits:
Object
  • Object
show all
Defined in:
lib/growl_notify_osascript.rb

Defined Under Namespace

Classes: GrowlNotFound

Constant Summary collapse

VERSION =
"0.0.1"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.application_nameObject

Returns the value of attribute application_name.



10
11
12
# File 'lib/growl_notify_osascript.rb', line 10

def application_name
  @application_name
end

.default_notificationsObject

Returns the value of attribute default_notifications.



10
11
12
# File 'lib/growl_notify_osascript.rb', line 10

def default_notifications
  @default_notifications
end

.iconObject

Returns the value of attribute icon.



10
11
12
# File 'lib/growl_notify_osascript.rb', line 10

def icon
  @icon
end

.notificationsObject

Returns the value of attribute notifications.



10
11
12
# File 'lib/growl_notify_osascript.rb', line 10

def notifications
  @notifications
end

Class Method Details

.config(&block) ⇒ Object



16
17
18
19
# File 'lib/growl_notify_osascript.rb', line 16

def config(&block)
  block.call(self)
  register
end

.emergency(options = {}) ⇒ Object



114
115
116
117
# File 'lib/growl_notify_osascript.rb', line 114

def emergency(options={})
  options.merge!(:priority => 2)
  send_notification(options)
end

.escape(string) ⇒ Object



21
22
23
# File 'lib/growl_notify_osascript.rb', line 21

def escape(string)
  '"' + string.to_s.gsub('"', '\"') + '"'
end

.high(options = {}) ⇒ Object



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

def high(options={})
  options.merge!(:priority => 1)
  send_notification(options)
end

.moderate(options = {}) ⇒ Object



99
100
101
102
# File 'lib/growl_notify_osascript.rb', line 99

def moderate(options={})
  options.merge!(:priority => -1)
  send_notification(options)
end

.normal(options = {}) ⇒ Object



104
105
106
107
# File 'lib/growl_notify_osascript.rb', line 104

def normal(options={})
  options.merge!(:priority => 0)
  send_notification(options)      
end

.registerObject

Raises:



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/growl_notify_osascript.rb', line 53

def register
  raise GrowlNotFound unless running?
  
  script = "tell application id \"com.Growl.GrowlHelperApp\"\n" + 
           "register " +
           to_applescript(
             :as_application => @application_name,
             :all_notifications => @notifications,
             :default_notifications => @default_notifications
           ) + 
           "\nend tell"
           
  run_script(script)
end

.reset!Object



34
35
36
37
38
# File 'lib/growl_notify_osascript.rb', line 34

def reset!
  [:application_name, :default_notifications, :notifications, :icon].each do |meth|
    send(:"#{meth}=", nil)
  end
end

.run_script(script) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/growl_notify_osascript.rb', line 25

def run_script(script)
 output = Open3.popen3('osascript') do |i, o, ts|
   i.puts script
   i.close
   o.gets
 end
  output ? output.strip : nil
end

.running?Boolean

Returns:

  • (Boolean)


68
69
70
71
72
73
# File 'lib/growl_notify_osascript.rb', line 68

def running?
  script = "tell application \"System Events\"\n" +
  	       "set isRunning to count of (every process whose bundle identifier is \"com.Growl.GrowlHelperApp\") > 0\n" +
           "end tell"
  run_script(script) == 'true'
end

.send_notification(options = {}) ⇒ Object

Raises:



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/growl_notify_osascript.rb', line 75

def send_notification(options= {})
  raise GrowlNotFound unless running?
  defaults = {:title => 'no title', :application_name => @application_name, :description => 'no description', :sticky => false, :priority => 0, :with_name => notifications.first}
  local_icon = @icon
  local_icon = options.delete(:icon) if options.include?(:icon)
  if local_icon
    defaults.merge!(:image_from_location => local_icon)
  end
  
  opts = defaults.merge(options)
  
  script = "tell application id \"com.Growl.GrowlHelperApp\"\n" + 
           "notify " +
           to_applescript(opts) +
           "\nend tell"
  
  run_script(script)
end

.sticky!(options = {}) ⇒ Object



119
120
121
122
# File 'lib/growl_notify_osascript.rb', line 119

def sticky!(options={})
  options.merge!(:sticky => true)
  send_notification(options)
end

.to_applescript(hash) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/growl_notify_osascript.rb', line 40

def to_applescript(hash)
  hash.map do |key, value|
    unless value.nil?
      name = key.to_s.gsub('_', ' ')
      if value.kind_of?(Array)
        "#{name} {" + value.map{|v| escape(v)}.join(', ') + "}" 
      else
        "#{name} " + escape(value)
      end
    end
  end.compact.join(' ')
end

.very_low(options = {}) ⇒ Object



94
95
96
97
# File 'lib/growl_notify_osascript.rb', line 94

def very_low(options={})
  options.merge!(:priority => -2)
  send_notification(options)
end