Class: Notifiers::Osascript

Inherits:
Base
  • Object
show all
Defined in:
lib/notifiers/osascript.rb

Overview

macOS native notifications using osascript (AppleScript).

Note: The ‘display notification’ AppleScript command does not support custom images. The notification will display the application icon of the calling process (e.g., Terminal.app or Ruby). If you need custom image support on macOS, consider using terminal-notifier instead.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

command?, inherited, #message, #notify, platform?, process?, subclasses, success_process_status?, #title

Class Method Details

.installed?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/notifiers/osascript.rb', line 11

def self.installed?
  platform?(/darwin/) and command?('osascript')
end

Instance Method Details

#image(icon) ⇒ Object

Override to warn that images are not supported by osascript.



16
17
18
19
20
21
# File 'lib/notifiers/osascript.rb', line 16

def image(icon)
  warn '[Notifiers::Osascript] Warning: Custom images are not supported ' \
       'by osascript display notification. The image will be ignored. ' \
       'Consider using terminal-notifier for image support.'
  super
end

#install_instructionsObject



43
44
45
# File 'lib/notifiers/osascript.rb', line 43

def install_instructions
  ''
end

#sound(name) ⇒ Object



28
29
30
31
# File 'lib/notifiers/osascript.rb', line 28

def sound(name)
  @sound = name
  self
end

#subtitle(text) ⇒ Object



23
24
25
26
# File 'lib/notifiers/osascript.rb', line 23

def subtitle(text)
  @subtitle = text
  self
end

#to_sObject



33
34
35
36
37
38
39
40
41
# File 'lib/notifiers/osascript.rb', line 33

def to_s
  script = 'display notification'.dup
  script << " \"#{@message}\"" if @message
  script << " with title \"#{@title}\"" if @title
  script << " subtitle \"#{@subtitle}\"" if @subtitle
  script << " sound name \"#{@sound}\"" if @sound

  "osascript -e '#{script}'"
end