Class: Pullbox::AppleScript
- Inherits:
-
Object
- Object
- Pullbox::AppleScript
- Defined in:
- lib/pullbox/applescript.rb
Overview
Applescript wrapper
Class Method Summary collapse
- .applications_folder ⇒ Object
- .display_dialog(message, title, defaults = {}) ⇒ Object
- .display_notification(message, title) ⇒ Object
- .export_playlist(name, to, format = :xml) ⇒ Object
- .intro ⇒ Object
- .move_app(name, app_path, launch = true) ⇒ Object
Class Method Details
.applications_folder ⇒ Object
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(, title, defaults = {}) defaults = { answer: false, buttons: %w[OK] }.merge(defaults) answer = defaults[:answer] ? ' default answer ""' : "" = defaults[:buttons].last = defaults[:buttons].first = 'buttons {"' = .dup << defaults[:buttons].join('", "') << "\"} default button \"#{}\" cancel button \"#{}\"" applescript = <<~APS.strip #{intro} try set theReturnedValue to (display dialog "#{}" #{}#{answer} with title "#{title}") if button returned of theReturnedValue is "#{}" 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(, title) applescript = <<~APS.strip #{intro} display notification "#{}" 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 |
.intro ⇒ Object
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 |