Class: GrowlNotifyOsascript
- Inherits:
-
Object
- Object
- GrowlNotifyOsascript
- Defined in:
- lib/growl_notify_osascript.rb
Defined Under Namespace
Classes: GrowlNotFound
Constant Summary collapse
- VERSION =
"0.0.1"
Class Attribute Summary collapse
-
.application_name ⇒ Object
Returns the value of attribute application_name.
-
.default_notifications ⇒ Object
Returns the value of attribute default_notifications.
-
.icon ⇒ Object
Returns the value of attribute icon.
-
.notifications ⇒ Object
Returns the value of attribute notifications.
Class Method Summary collapse
- .config(&block) ⇒ Object
- .emergency(options = {}) ⇒ Object
- .escape(string) ⇒ Object
- .high(options = {}) ⇒ Object
- .moderate(options = {}) ⇒ Object
- .normal(options = {}) ⇒ Object
- .register ⇒ Object
- .reset! ⇒ Object
- .run_script(script) ⇒ Object
- .running? ⇒ Boolean
- .send_notification(options = {}) ⇒ Object
- .sticky!(options = {}) ⇒ Object
- .to_applescript(hash) ⇒ Object
- .very_low(options = {}) ⇒ Object
Class Attribute Details
.application_name ⇒ Object
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_notifications ⇒ Object
Returns the value of attribute default_notifications.
10 11 12 |
# File 'lib/growl_notify_osascript.rb', line 10 def default_notifications @default_notifications end |
.icon ⇒ Object
Returns the value of attribute icon.
10 11 12 |
# File 'lib/growl_notify_osascript.rb', line 10 def icon @icon end |
.notifications ⇒ Object
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(={}) .merge!(:priority => 2) send_notification() 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(={}) .merge!(:priority => 1) send_notification() end |
.moderate(options = {}) ⇒ Object
99 100 101 102 |
# File 'lib/growl_notify_osascript.rb', line 99 def moderate(={}) .merge!(:priority => -1) send_notification() end |
.normal(options = {}) ⇒ Object
104 105 106 107 |
# File 'lib/growl_notify_osascript.rb', line 104 def normal(={}) .merge!(:priority => 0) send_notification() end |
.register ⇒ Object
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
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
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(= {}) 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 = .delete(:icon) if .include?(:icon) if local_icon defaults.merge!(:image_from_location => local_icon) end opts = defaults.merge() 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!(={}) .merge!(:sticky => true) send_notification() 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(={}) .merge!(:priority => -2) send_notification() end |