Last active
August 29, 2015 14:09
-
-
Save jenrzzz/980fed34dab65a6c74ae to your computer and use it in GitHub Desktop.
Powerline segment for iTunes now playing
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
class iTunesPlayerSegment(PlayerSegment): | |
def get_player_status(self, pl): | |
status_delimiter = '-~`/=' | |
ascript = ''' | |
tell application "System Events" | |
set process_list to (name of every process) | |
end tell | |
if process_list contains "iTunes" then | |
tell application "iTunes" | |
if player state is playing then | |
set t_title to name of current track | |
set t_artist to artist of current track | |
set t_album to album of current track | |
set t_duration to duration of current track | |
set t_elapsed to player position | |
set t_state to player state | |
return t_title & "{0}" & t_artist & "{0}" & t_album & "{0}" & t_elapsed & "{0}" & t_duration & "{0}" & t_state | |
end if | |
end tell | |
end if | |
'''.format(status_delimiter) | |
now_playing = asrun(pl, ascript) | |
if not now_playing: | |
return | |
now_playing = now_playing.split(status_delimiter) | |
if len(now_playing) != 6: | |
return | |
title, artist = now_playing[0], now_playing[1] | |
title = title[0:25] + "…" if len(title) > 25 else title | |
artist = artist[0:15] + "…" if len(artist) > 15 else artist | |
state = _convert_state(now_playing[5]) | |
total = _convert_seconds(now_playing[4]) | |
elapsed = _convert_seconds(float(now_playing[4]) - float(now_playing[4])) | |
return { | |
'title': title, | |
'artist': now_playing[1], | |
'album': now_playing[2], | |
'total': total, | |
'elapsed': elapsed, | |
'state': state | |
} | |
itunes = with_docstring(iTunesPlayerSegment(), | |
('''Return iTunes now playing information. | |
Requires ``osascript``. | |
{0} | |
''').format(_common_args.format('itunes'))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment