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 = <<-APS.strip
    #{intro}

    set theApplicationsFolder to path to applications folder
    return (POSIX path) of theApplicationsFolder
  APS
  `osascript -e '#{applescript}'`.strip
end

.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 = <<~APS.strip
    #{intro}
    try
      set theReturnedValue to (display dialog "#{message}" #{button_construct}#{answer} with title "#{title}")
      if button returned of theReturnedValue is "#{default_button}" then
        if text returned of theReturnedValue is not "" then
          return text returned of theReturnedValue
        else
          return
        end if
      end if
    on error errorMessage number errorNumber
      if errorNumber is equal to -128 -- aborted by user
        return
      end if
    end try
  APS
  answer = `osascript -e '#{applescript}'`.strip

  exit(0) if answer.empty?

  answer
end

.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 = <<~APS.strip
    #{intro}
    display notification "#{message}" with title "#{title}"
  APS
  system "osascript -e '#{applescript}'"
end

.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 = <<~APS.strip
    #{intro}
    tell application "Music" to export playlist "#{name}" as #{formats.fetch(format, 'XML')} to "#{to}"
  APS
  system "osascript -e '#{applescript}'"
end

.introObject



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

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

  APS
end

.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 = <<-APS.strip
    #{intro}

    set theApplicationsFolder to path to applications folder
    try
      tell application "#{name}" to quit
    on error errMsg
    end try
    delay 3
    tell application "Finder"
      move (POSIX file "#{app_path}") as alias to theApplicationsFolder with replacing
    end tell
    if #{launch} then
      tell application "#{name}" to activate
    end if
  APS
  system "osascript -e '#{applescript}'"
end