Class: SpotifyCli::App

Inherits:
Object
  • Object
show all
Defined in:
lib/spotify_cli/app.rb

Class Method Summary collapse

Class Method Details

.next!Object



61
62
63
# File 'lib/spotify_cli/app.rb', line 61

def self.next!
  oascript('tell application "Spotify" to next track')
end

.oascript(command) ⇒ Object



97
98
99
# File 'lib/spotify_cli/app.rb', line 97

def self.oascript(command)
  `osascript -e '#{command}'`.strip
end

.pause!Object



53
54
55
# File 'lib/spotify_cli/app.rb', line 53

def self.pause!
  oascript('tell application "Spotify" to pause')
end

.percent_done(position, duration) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/spotify_cli/app.rb', line 82

def self.percent_done(position, duration)
  seconds = ->(parts) do
    acc = 0
    multiplier = 1
    while part = parts.shift
      acc += part.to_f * multiplier
      multiplier *= 60
    end
    acc
  end
  pos_parts = position.split(':').reverse
  dur_parts = duration.split(':').reverse
  seconds.call(pos_parts) / seconds.call(dur_parts)
end

.play_pause!Object



49
50
51
# File 'lib/spotify_cli/app.rb', line 49

def self.play_pause!
  oascript('tell application "Spotify" to playpause')
end

.play_uri!(uri) ⇒ Object



57
58
59
# File 'lib/spotify_cli/app.rb', line 57

def self.play_uri!(uri)
  oascript("tell application \"Spotify\" to play track \"#{uri}\"")
end

.previous!Object



69
70
71
72
73
74
75
76
# File 'lib/spotify_cli/app.rb', line 69

def self.previous!
  oascript(<<-EOF)
  tell application "Spotify"
      set player position to 0
      previous track
  end tell
  EOF
end

.replay!Object



78
79
80
# File 'lib/spotify_cli/app.rb', line 78

def self.replay!
  oascript('tell application "Spotify" to set player position to 0')
end

.set_pos!(pos) ⇒ Object



65
66
67
# File 'lib/spotify_cli/app.rb', line 65

def self.set_pos!(pos)
  oascript("tell application \"Spotify\" to set player position to #{pos}")
end

.stateObject



3
4
5
# File 'lib/spotify_cli/app.rb', line 3

def self.state
  oascript('tell application "Spotify" to player state as string')
end

.statusObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
# File 'lib/spotify_cli/app.rb', line 7

def self.status
  artist = oascript('tell application "Spotify" to artist of current track as string')
  album = oascript('tell application "Spotify" to album of current track as string')
  track = oascript('tell application "Spotify" to name of current track as string')
  duration = oascript(<<-EOF)
    tell application "Spotify"
    set durSec to (duration of current track / 1000) as text
    set tM to (round (durSec / 60) rounding down) as text
    if length of ((durSec mod 60 div 1) as text) is greater than 1 then
        set tS to (durSec mod 60 div 1) as text
    else
        set tS to ("0" & (durSec mod 60 div 1)) as text
    end if
    set myTime to tM as text & ":" & tS as text
    end tell
    return myTime
  EOF
  position = oascript(<<-EOF)
    tell application "Spotify"
    set pos to player position
    set nM to (round (pos / 60) rounding down) as text
    if length of ((round (pos mod 60) rounding down) as text) is greater than 1 then
        set nS to (round (pos mod 60) rounding down) as text
    else
        set nS to ("0" & (round (pos mod 60) rounding down)) as text
    end if
    set nowAt to nM as text & ":" & nS as text
    end tell
    return nowAt
  EOF

  {
    state: state,
    artist: artist,
    album: album,
    track: track,
    duration: duration,
    position: position,
    percent_done: percent_done(position, duration)
  }
end