bitbar plugin for the currently playing Spotify song
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
#!/bin/bash | |
spotifycache="/Users/david/Library/Application Support/Spotify/PersistentCache/Storage" | |
if [[ "$1" = "open" ]]; then | |
open /Applications/Spotify.app | |
fi | |
if [[ "$1" = "copy" ]]; then | |
echo "$2"| pbcopy | |
fi | |
if [[ "$1" = "showcache" ]]; then | |
open "$spotifycache" | |
fi | |
function playpause(){ | |
osascript -l JavaScript<<'END' | |
const spotify = Application('spotify') | |
if(spotify.running()){ | |
try{ | |
spotify.playpause() | |
} | |
catch(e){'💚'} | |
} | |
else '💚' | |
END | |
} | |
if [[ "$1" = "playpause" ]]; then | |
playpause | |
fi | |
function nowplayingcore(){ | |
osascript -l JavaScript<<'END' | |
const spotify = Application('spotify') | |
if(spotify.running()){ | |
try{ | |
const { name, artist } = spotify.currentTrack | |
if(artist().length>0)`${name()} - ${artist()}`.split('|').join('∣') | |
else if (name().length>0) name().split('|').join('∣') | |
else '💚' | |
} | |
catch(e){'💚'} | |
} | |
else '💚' | |
END | |
} | |
function nowplayingartwork(){ | |
osascript -l JavaScript<<'END' | |
const spotify = Application('spotify') | |
if(spotify.running()){ | |
try{ | |
const { artworkUrl } = spotify.currentTrack | |
artworkUrl() | |
} | |
catch(e){'💚';e} | |
} | |
else '💚' | |
END | |
} | |
function playbutton(){ | |
osascript -l JavaScript<<'END' | |
const spotify = Application('spotify') | |
if(spotify.running()){ | |
try{ | |
const state = spotify.playerState() | |
if(state=='playing')'Pause ⏸' | |
else 'Play ▶️' | |
} | |
catch(e){e} | |
} | |
else '💚' | |
END | |
} | |
function nowplaying(){ | |
np=$(nowplayingcore) | |
if [[ $np ]];then | |
echo "$np" | |
echo "---" | |
echo "Open Spotify|bash=$0 param1=open terminal=false" | |
echo "Play/Pause|bash=$0 param1=playpause terminal=false" | |
echo "Cache size: $(du -ch "$spotifycache" | grep total)|bash=$0 param1=showcache terminal=false" | |
fi | |
# np=$(playbutton) | |
# if [[ $np ]];then | |
# echo "$np|bash=$0 param1=playpause terminal=false refresh=true" | |
# fi | |
np=$(nowplayingartwork) | |
if [[ $np ]];then | |
echo "Copy artwork URL|bash=$0 param1=copy param2=$np terminal=false" | |
fi | |
} | |
dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd ) | |
nowplaying |
Updated to also show cache size (requires editing), and links to cache to flush manually
Now escapes |
in artist/track name and replaces with ∣
(U+2223)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Newest version uses JXA (JavaScript for automation) rather than AppleScript, which is much cleaner