-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcurrent_song.rb
More file actions
executable file
·33 lines (27 loc) · 1005 Bytes
/
current_song.rb
File metadata and controls
executable file
·33 lines (27 loc) · 1005 Bytes
1
2
3
4
5
6
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
#!/usr/bin/env ruby
def status(app, var)
exec_script(app, "#{var} as string")
end
def exec_script(app, command)
script = `osascript -e 'tell application \"#{app}\" to #{command}'`.gsub("\n", "")
end
def show_track(data)
return if data.nil?
puts "#{data[:artist]} - #{data[:track]} (#{data[:state]})"
end
def find_app(info, state)
app = info.select { |k, v| v[:running] && v[:state] == state }
return nil if app.empty?
app.first.last
end
info = ["Rdio","Spotify", "iTunes"].inject({}) do |hash, app|
hash[app] = {:running => exec_script("System Events", "count (every process whose name is \"#{app}\")").to_i == 1}
if hash[app][:running]
hash[app].merge!(:state => status(app, "player state"))
if hash[app][:state] == "paused" || hash[app][:state] == "playing"
hash[app].merge!(:artist => status(app, "artist of current track"), :track => status(app, "name of current track"))
end
end
hash
end
show_track(find_app(info, "playing") || find_app(info, "paused"))