Skip to content

Instantly share code, notes, and snippets.

@davidsharp
Last active May 20, 2022 12:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidsharp/767f69c090c7381af70ff50325f56bca to your computer and use it in GitHub Desktop.
Save davidsharp/767f69c090c7381af70ff50325f56bca to your computer and use it in GitHub Desktop.
bitbar plugin for the currently playing Spotify song
#!/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
@davidsharp
Copy link
Author

Newest version uses JXA (JavaScript for automation) rather than AppleScript, which is much cleaner

@davidsharp
Copy link
Author

Updated to also show cache size (requires editing), and links to cache to flush manually

@davidsharp
Copy link
Author

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