Last active
May 20, 2022 12:57
-
-
Save davidsharp/767f69c090c7381af70ff50325f56bca to your computer and use it in GitHub Desktop.
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