Powerline: 添加 iTunes 正在播放片段。

创建于 2016-05-10  ·  5评论  ·  资料来源: powerline/powerline

我希望我可以在 OS X 上从 iTunes 添加正在播放的片段。我在 Google 中找到了显示当前音乐的片段的要点,但我不知道如何安装它(甚至不知道如何测试它)。 你能启发我吗?

segment players macos enhancement

最有用的评论

没关系,我找到了解决方案并测试了该要点。 只需对转换浮点变量进行一些修复(AppleScript 用逗号而不是点返回“浮点”,所以我只是将“,”替换为“。”并转换为浮点数)。 这是固定脚本,只需将其添加到/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')))

而且看起来 . 我希望它会帮助某人😄

所有5条评论

没关系,我找到了解决方案并测试了该要点。 只需对转换浮点变量进行一些修复(AppleScript 用逗号而不是点返回“浮点”,所以我只是将“,”替换为“。”并转换为浮点数)。 这是固定脚本,只需将其添加到/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')))

而且看起来 . 我希望它会帮助某人😄

不知何故,我现在仍然无法让 iTunes 显示在我的 tmux 状态行中。

  1. 我将上述脚本附加到powerline/segments/common/players.py
  2. 在我的 tmux 主题文件中:
{
  "segments": {
    "right": [
      {
        "function": "powerline.segments.common.players.itunes",
        "name": "player"
      },
      ...
    ],
    ...
  }
}
  1. powerline-lint显示没有错误。
  2. powerline-config tmux setup (带有log_level=DEBUG )不显示与玩家相关的任何内容。

我对我的 tmux 主题进行了其他更改,并看到它们反映在我的状态栏中,但现在仍然没有 iTunes 正在播放。
我测试了上面脚本中的applescript,以确保它返回所需的字符串,这似乎没问题。

我的电力线配置中是否遗漏了什么?

@eliath您是否重新启动了电力线守护进程? “我......看到它们反映在我的状态栏中”是不相关的:实际上 powerline 应该在更改后重新加载其配置,这有时会起作用。 但它不会重新加载任何 python 代码。

是的,就是这样。 重新加载我的 tmux 配置并重新启动守护程序后,一切都按预期工作。 谢谢!

这是前一段时间从#1732 合并的。

此页面是否有帮助?
0 / 5 - 0 等级