Class: Pullbox::AppleScript

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

Overview

Applescript wrapper

Class Method Summary collapse

Class Method Details

.applications_folderObject



72
73
74
75
76
77
78
79
80
# File 'lib/pullbox/applescript.rb', line 72

def self.applications_folder
  applescript = "    \#{intro}\n\n    set theApplicationsFolder to path to applications folder\n    return (POSIX path) of theApplicationsFolder\n  APS\n  `osascript -e '\#{applescript}'`.strip\nend\n".strip

.display_dialog(message, title, defaults = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/pullbox/applescript.rb', line 23

def self.display_dialog(message, title, defaults = {})
  defaults = { answer: false, buttons: %w[OK] }.merge(defaults)
  answer = defaults[:answer] ? ' default answer ""' : ""
  default_button = defaults[:buttons].last
  cancel_button = defaults[:buttons].first
  buttons = 'buttons {"'
  button_construct = buttons.dup
button_construct << defaults[:buttons].join('", "')
  button_construct << "\"} default button \"#{default_button}\" cancel button \"#{cancel_button}\""
  applescript = "    \#{intro}\n    try\n      set theReturnedValue to (display dialog \"\#{message}\" \#{button_construct}\#{answer} with title \"\#{title}\")\n      if button returned of theReturnedValue is \"\#{default_button}\" then\n        if text returned of theReturnedValue is not \"\" then\n          return text returned of theReturnedValue\n        else\n          return\n        end if\n      end if\n    on error errorMessage number errorNumber\n      if errorNumber is equal to -128 -- aborted by user\n        return\n      end if\n    end try\n  APS\n  answer = `osascript -e '\#{applescript}'`.strip\n\n  exit(0) if answer.empty?\n\n  answer\nend\n".strip

.display_notification(message, title) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/pullbox/applescript.rb', line 15

def self.display_notification(message, title)
  applescript = "    \#{intro}\n    display notification \"\#{message}\" with title \"\#{title}\"\n  APS\n  system \"osascript -e '\#{applescript}'\"\nend\n".strip

.export_playlist(name, to, format = :xml) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/pullbox/applescript.rb', line 56

def self.export_playlist(name, to, format = :xml)
  formats = {
    m3u: "M3U",
    m3u8: "M3U8",
    plain_text: "plain text",
    unicode_text: "Unicode text",
    xml: "XML"
  }

  applescript = "    \#{intro}\n    tell application \"Music\" to export playlist \"\#{name}\" as \#{formats.fetch(format, 'XML')} to \"\#{to}\"\n  APS\n  system \"osascript -e '\#{applescript}'\"\nend\n".strip

.introObject



7
8
9
10
11
12
13
# File 'lib/pullbox/applescript.rb', line 7

def self.intro
  "    use AppleScript version \"2.4\" -- Yosemite (10.10) or later\n    use scripting additions\n\n  APS\nend\n".strip

.move_app(name, app_path, launch = true) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/pullbox/applescript.rb', line 82

def self.move_app(name, app_path, launch = true)
  applescript = "    \#{intro}\n\n    set theApplicationsFolder to path to applications folder\n    try\n      tell application \"\#{name}\" to quit\n    on error errMsg\n    end try\n    delay 3\n    tell application \"Finder\"\n      move (POSIX file \"\#{app_path}\") as alias to theApplicationsFolder with replacing\n    end tell\n    if \#{launch} then\n      tell application \"\#{name}\" to activate\n    end if\n  APS\n  system \"osascript -e '\#{applescript}'\"\nend\n".strip