From 6e0fe442de205966a19b550b09c961876374c3e2 Mon Sep 17 00:00:00 2001 From: Eric Date: Wed, 29 Jan 2014 22:41:24 -0600 Subject: [PATCH] Port spotify mac script from tmux-powerline --- .../colorschemes/tmux/default.json | 3 +- powerline/lib/apple_script_runner.py | 13 +++++ powerline/segments/common.py | 58 ++++++++++++++++++- 3 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 powerline/lib/apple_script_runner.py diff --git a/powerline/config_files/colorschemes/tmux/default.json b/powerline/config_files/colorschemes/tmux/default.json index d418473c..b718a2ed 100644 --- a/powerline/config_files/colorschemes/tmux/default.json +++ b/powerline/config_files/colorschemes/tmux/default.json @@ -24,6 +24,7 @@ "cpu_load_percent_gradient": { "fg": "green_yellow_orange_red", "bg": "gray0" }, "environment": { "fg": "gray8", "bg": "gray0" }, "battery": { "fg": "gray8", "bg": "gray0" }, - "battery_gradient": { "fg": "white_red", "bg": "gray0" } + "battery_gradient": { "fg": "white_red", "bg": "gray0" }, + "now_playing": { "fg": "gray10", "bg": "black" } } } diff --git a/powerline/lib/apple_script_runner.py b/powerline/lib/apple_script_runner.py new file mode 100644 index 00000000..d544e16e --- /dev/null +++ b/powerline/lib/apple_script_runner.py @@ -0,0 +1,13 @@ +# vim:fileencoding=utf-8:noet + +def asrun(ascript): + '''Run the given AppleScript and return the standard output and error.''' + from subprocess import Popen, PIPE + osa = Popen(['osascript', '-'], stdin=PIPE, stdout=PIPE) + return osa.communicate(ascript)[0] + +def asquote(astr): + '''Return the AppleScript equivalent of the given string.''' + + astr = astr.replace('"', '" & quote & "') + return '"{}"'.format(astr) diff --git a/powerline/segments/common.py b/powerline/segments/common.py index b5cd1016..d4f47937 100644 --- a/powerline/segments/common.py +++ b/powerline/segments/common.py @@ -10,6 +10,7 @@ import socket from multiprocessing import cpu_count as _cpu_count from powerline.lib import add_divider_highlight_group +from powerline.lib.apple_script_runner import asrun, asquote from powerline.lib.url import urllib_read, urllib_urlencode from powerline.lib.vcs import guess, tree_status from powerline.lib.threaded import ThreadedSegment, KwThreadedSegment, with_docstring @@ -983,7 +984,7 @@ class NowPlayingSegment(object): 'total': now_playing[3], } - def player_spotify(self, pl): + def player_spotify_dbus(self, pl, dbus=None): try: import dbus except ImportError: @@ -1011,6 +1012,61 @@ class NowPlayingSegment(object): 'total': self._convert_seconds(info.get('mpris:length') / 1e6), } + def player_spotify_apple_script(self, pl): + ascript = ''' + tell application "System Events" + set process_list to (name of every process) + end tell + + if process_list contains "Spotify" then + tell application "Spotify" + if player state is playing or player state is paused then + set track_name to name of current track + set artist_name to artist of current track + set album_name to album of current track + set track_length to duration of current track + set trim_length to 40 + set now_playing to player state & album_name & artist_name & track_name & track_length + if length of now_playing is less than trim_length then + set now_playing_trim to now_playing + else + set now_playing_trim to characters 1 thru trim_length of now_playing as string + end if + else + return player state + end if + + end tell + else + return "stopped" + end if + ''' + + spotify = asrun(ascript) + + spotify_status = spotify.split(", ") + state = self._convert_state(spotify_status[0]) + if state == 'stop': + return + return { + 'state': state, + 'state_symbol': self.STATE_SYMBOLS.get(state), + 'album': spotify_status[1], + 'artist': spotify_status[2], + 'title': spotify_status[3], + 'total': self._convert_seconds(int(spotify_status[4])) + } + + try: + __import__('dbus') # NOQA + except ImportError: + if sys.platform.startswith('darwin'): + player_spotify = player_spotify_apple_script + else: + player_spotify = player_spotify_dbus # NOQA + else: + player_spotify = player_spotify_dbus # NOQA + def player_rhythmbox(self, pl): now_playing = self._run_cmd(['rhythmbox-client', '--no-start', '--no-present', '--print-playing-format', '%at\n%aa\n%tt\n%te\n%td']) if not now_playing: