Skip to content

Instantly share code, notes, and snippets.

@davidsharp
Last active May 20, 2022
Embed
What would you like to do?
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

davidsharp commented Dec 16, 2019

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

@davidsharp
Copy link
Author

davidsharp commented May 24, 2021

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

@davidsharp
Copy link
Author

davidsharp commented May 20, 2022

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