Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add iTunes now playing segment. #1594

Closed
pklebba opened this issue May 10, 2016 · 5 comments
Closed

Add iTunes now playing segment. #1594

pklebba opened this issue May 10, 2016 · 5 comments

Comments

@pklebba
Copy link

pklebba commented May 10, 2016

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?

@pklebba
Copy link
Author

pklebba commented May 10, 2016

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 😄

@eliath
Copy link

eliath commented Jan 31, 2017

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?

@ZyX-I
Copy link
Contributor

ZyX-I commented Jan 31, 2017

@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.

@ZyX-I ZyX-I added the e:macos label Jan 31, 2017
@eliath
Copy link

eliath commented Feb 6, 2017

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

@ZyX-I ZyX-I closed this as completed Nov 11, 2018
@ZyX-I
Copy link
Contributor

ZyX-I commented Nov 11, 2018

This was merged some time ago from #1732.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants