Created
March 28, 2012 18:45
-
-
Save eric-wood/2229326 to your computer and use it in GitHub Desktop.
iTunes track information in your terminal!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Show iTunes information in real-time in the terminal | |
def status | |
cmd = "osascript -e 'tell application \"iTunes\" to player state'" | |
`#{cmd}` | |
end | |
def track_name | |
cmd = "osascript -e 'tell application \"iTunes\" to name of current track'" | |
`#{cmd}` | |
end | |
def track_artist | |
cmd = "osascript -e 'tell application \"iTunes\" to artist of current track'" | |
`#{cmd}` | |
end | |
def track_album | |
cmd = "osascript -e 'tell application \"iTunes\" to album of current track'" | |
`#{cmd}` | |
end | |
# Return how far we are into the current track | |
def position | |
cmd = "osascript -e 'tell application \"iTunes\" to player position'" | |
pos = `#{cmd}` | |
pos.to_i | |
end | |
def position_string(pos) | |
frac = (pos.to_i)/(60.to_f) | |
min = frac.to_i | |
sec = ((frac - min)*60).ceil | |
min = min.to_s | |
sec = sec.to_s | |
min = ' ' + min if min.size == 1 | |
sec = '0' + sec if sec.size == 1 | |
"#{min}:#{sec}" | |
end | |
def track_time | |
cmd = "osascript -e 'tell application \"iTunes\" to time of current track'" | |
`#{cmd}`.to_f | |
end | |
def track_length | |
cmd = "osascript -e 'tell application \"iTunes\" to duration of current track'" | |
len = `#{cmd}` | |
len.to_i | |
end | |
progress_width = 30 | |
tick_size = 0 | |
len = 0 | |
pos = 0 | |
tick_char = "=" | |
prev_track = "" | |
loop do | |
st = status.strip | |
if st == 'playing' | |
name = track_name | |
if name != prev_track | |
# Get info | |
artist = track_artist | |
album = track_album | |
len = track_length | |
pos = position | |
rem = (len - pos).abs | |
rem_string = position_string(rem) | |
pos_string = position_string(pos) | |
tick_size = len / progress_width | |
# Display info | |
print "\e[1A\e[2K"*4 | |
puts name, artist, album | |
puts "#{pos_string} [#{' '*progress_width}] #{rem_string}" | |
else | |
pos = position | |
rem = (len - pos) | |
pos_string = position_string(pos) | |
rem_string = position_string(rem) | |
ticks = (pos / tick_size).floor | |
spaces = progress_width - ticks | |
print "\e[1A\e[2K" | |
puts "#{pos_string} [#{tick_char*ticks}#{' '*spaces}] #{rem_string}" | |
end | |
prev_track = track_name | |
else | |
prev_track = "" | |
end | |
sleep 0.1 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Huh, that's nifty. Thanks, I didn't know about those!