Powerline: Add iTunes now playing segment.

Created on 10 May 2016  ·  5Comments  ·  Source: powerline/powerline

I wish I could add now playing segment from iTunes on OS X. I found gist in Google with segment what shows current music, but I don't know how to install it (or even test it). Can you enlighten me?

segment players macos enhancement

Most helpful comment

Nevermind, I found a solution and I tested that gist. It was needed just a little fix with converting float variable (AppleScript returns "float" with comma instead of dot, so I just replaced "," to "." and converted to float). This is fixed script, just add it to /powerline/segments/common/players.py.

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 or player state is paused 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

        now_playing[4] = now_playing[4].replace(',', '.')
        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')))

And it looks that . I hope it will help someone 😄

All 5 comments

Nevermind, I found a solution and I tested that gist. It was needed just a little fix with converting float variable (AppleScript returns "float" with comma instead of dot, so I just replaced "," to "." and converted to float). This is fixed script, just add it to /powerline/segments/common/players.py.

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 or player state is paused 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

        now_playing[4] = now_playing[4].replace(',', '.')
        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')))

And it looks that . I hope it will help someone 😄

Somehow I still can't get itunes now playing to show up in my tmux statusline.

  1. I append the above script to powerline/segments/common/players.py.
  2. In my tmux theme file:
{
  "segments": {
    "right": [
      {
        "function": "powerline.segments.common.players.itunes",
        "name": "player"
      },
      ...
    ],
    ...
  }
}
  1. powerline-lint shows no errors.
  2. powerline-config tmux setup (with log_level=DEBUG) shows nothing related to the player.

I have made other changes to my tmux theme and see them reflected in my statusbar, but still no itunes now playing.
I tested the applescript from the above script to make sure it returns the desired string, which seems to be ok.

Am I missing something in my powerline configuration?

@eliath Did you restart the powerline daemon? “I … see them reflected in my statusbar” is not relevant: actually powerline is supposed to reload its configuration after changes and this sometimes work. But it won’t reload any python code.

Yep, that was it. After reloading my tmux config and restarting the daemon, everything works as expected. Thanks!

This was merged some time ago from #1732.

Was this page helpful?
0 / 5 - 0 ratings