Last active
September 4, 2019 13:04
-
-
Save jamesthomasonjr/70ff0d021395b6f6a5f474bbcf1b4612 to your computer and use it in GitHub Desktop.
Add "Now Playing" information from Spotify to iTerm2 Status Bar
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
# ~/Library/Application Support/iTerm2/Scripts/AutoLaunch/spotify.py | |
# | |
# This script adds a new Status Bar component to iTerm2 that displays now playing information from Spotify | |
from subprocess import Popen, PIPE | |
import iterm2 | |
PREFIX = 'PREFIX' | |
TEMPLATE = 'TEMPLATE' | |
async def main(connection): | |
async def run_spotify_query(query): | |
process = Popen(['osascript', '-'] + [], stdin=PIPE, stdout=PIPE, stderr=PIPE, universal_newlines=True) | |
stdout, stderr = process.communicate(query) | |
# return stdout.strip() if not stderr else stderr.strip() | |
return stdout.strip() if not stderr else "Unknown" | |
component = iterm2.StatusBarComponent( | |
short_description="Spotify (now playing)", | |
detailed_description="View the currently playing song on spotify", | |
knobs=[ | |
iterm2.StringKnob('Prefix', '>', '', PREFIX), | |
iterm2.StringKnob('Template', "{artist} - {song}", "{artist} - {song}", TEMPLATE), | |
], | |
exemplar="Jonathan Coulton - Code Monkey", | |
# @TODO: Get this from config | |
update_cadence=2, | |
identifier="dev.j80.iterm2.component.spotify", | |
# @TODO: Add an icon | |
icons=[], | |
) | |
@iterm2.StatusBarRPC | |
async def get_spotify_now_playing(knobs=[]): | |
query = """ | |
on querySpotify(field) | |
if application "Spotify" is not running then | |
return "Unknown" | |
end if | |
tell application "Spotify" | |
if (current track = {{}}) then | |
return "Unknown" | |
end if | |
end tell | |
tell application "Spotify" | |
if (field = "artist") then | |
return (artist of current track) as string | |
end if | |
if (field = "song") then | |
return (name of current track) as string | |
end if | |
if (field = "album") then | |
return (album of current track) as string | |
end if | |
if (field = "position") then | |
return (player position) as string | |
end if | |
if (field = "duration") then | |
return (duration of current track) as string | |
end if | |
if (field = "state") then | |
return (player state) as string | |
end if | |
end tell | |
return "Unknown" | |
end querySpotify | |
querySpotify("{field}") | |
""" | |
# Note: Here are the available commands, might be useful for a popup menu later | |
# next track | |
# pause | |
# play | |
# play track | |
# playpause | |
# previous track | |
# quit | |
artist_query = query.format(field="artist") | |
song_query = query.format(field="song") | |
album_query = query.format(field="album") | |
position_query = query.format(field="position") | |
duration_query = query.format(field="duration") | |
state_query = query.format(field="state") | |
artist = await run_spotify_query(artist_query) | |
song = await run_spotify_query(song_query) | |
album = await run_spotify_query(album_query) | |
position = await run_spotify_query(position_query) | |
duration = await run_spotify_query(duration_query) | |
state = await run_spotify_query(state_query) | |
prefix = knobs[PREFIX] if PREFIX in knobs else '' | |
prefix = f'{prefix}' if prefix else '' | |
template = knobs[TEMPLATE] if TEMPLATE in knobs else "{artist} - {song}" | |
return f'{prefix}{template}'.format( | |
artist=artist, | |
song=song, | |
album=album, | |
position=position, | |
duration=duration, | |
state=state, | |
) | |
await component.async_register(connection, get_spotify_now_playing) | |
iterm2.run_forever(main) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment