Skip to content

Instantly share code, notes, and snippets.

@chew-z
Last active March 15, 2024 13:19
Show Gist options
  • Save chew-z/8aace29ed17f13bfa6adfe06319faf93 to your computer and use it in GitHub Desktop.
Save chew-z/8aace29ed17f13bfa6adfe06319faf93 to your computer and use it in GitHub Desktop.
Music plugin for Swiftbar
#!/bin/zsh
# metadata
# <xbar.title>Music Now Playing</xbar.title>
# <xbar.version>v1.2</xbar.version>
# <xbar.author>Dan Turkel, Jason Tokoph, Aleš Farčnik, Jeffrey Munowitch</xbar.author>
# <xbar.author.github>chew-z</xbar.author.github>
# <xbar.desc>Display currently playing Music song with artwork. Play/pause, skip forward, skip backward.</xbar.desc>
# <xbar.dependencies>zsh, javascript, JXA</xbar.dependencies>
# <xbar.abouturl>https://gist.github.com/chew-z/8aace29ed17f13bfa6adfe06319faf93</xbar.abouturl>
# <xbar.image>http://i.imgur.com/lBfoFdY.png</xbar.image>
if [ "$1" = 'launch' ]; then
osascript -l JavaScript -e 'Application("Music").open()'
exit
fi
if [ "$1" = 'open' ]; then
osascript -l JavaScript -e 'const music = Application("Music")
music.activate()
var playlist = music.playlists.byName("Music")
music.shuffleEnabled = true
playlist.reveal();
playlist.play()'
exit
fi
if [ "$(osascript -l JavaScript -e 'Application("Music").running()')" = "false" ]; then
echo "♫ | color=red"
echo "---"
echo "Music is closed | color=red"
echo "Open Music | bash='$0' param1=open terminal=False"
exit
fi
if [ "$1" = 'play' ]; then
state=$(osascript -l JavaScript -e 'Application("Music").playpause()');
fi
if [ "$1" = 'pause' ]; then
osascript -l JavaScript -e '
const music = Application("Music")
var initialVolume = music.soundVolume()
while (music.soundVolume() > 0) {
music.soundVolume = music.soundVolume() - 1
delay(0.02)
}
music.playpause()
music.soundVolume = initialVolume';
exit
fi
if [ "$1" = 'previous' ]; then
osascript -l JavaScript -e '
const music = Application("Music")
var initialVolume = music.soundVolume()
while (music.soundVolume() > 0) {
music.soundVolume = music.soundVolume() - 1
delay(0.04)
}
music.previousTrack()
music.soundVolume = initialVolume';
exit
fi
if [ "$1" = 'next' ]; then
osascript -l JavaScript -e '
const music = Application("Music")
var initialVolume = music.soundVolume()
while (music.soundVolume() > 0) {
music.soundVolume = music.soundVolume() - 1
delay(0.04)
}
music.nextTrack()
music.soundVolume = initialVolume';
exit
fi
state=$(osascript -l JavaScript -e 'Application("Music").playerState()');
# echo ":tray: $state"
if [ $state = "not available" ]; then
echo "♫ | color=red "
echo "---"
echo "Music is not available | color=red"
exit
fi
track=$(osascript -l JavaScript -e 'Application("Music").currentTrack.name()');
artist=$(osascript -l JavaScript -e 'Application("Music").currentTrack.artist()');
album=$(osascript -l JavaScript -e 'Application("Music").currentTrack.album()');
playlist=$(osascript -l JavaScript -e 'Application("Music").currentPlaylist.name()');
description=$(osascript -l JavaScript -e 'Application("Music").currentPlaylist.description()');
tmp_file=$(osascript -e'
try
tell application "Music"
tell artwork 1 of current track
if format is JPEG picture then
set imgFormat to ".jpg"
else
set imgFormat to ".png"
end if
end tell
set albumName to album of current track
set albumArtist to album artist of current track
if length of albumArtist is 0
set albumArtist to artist of current track
end if
set fileName to (do shell script "echo " & quoted form of albumArtist & quoted form of albumName & " | sed \"s/[^a-zA-Z0-9]//g\"") & imgFormat
-- set fileName to (do shell script "echo " & quoted form of albumArtist & " - " & quoted form of albumName & " | sed \"s/[[:space:]]//g\"") & imgFormat
end tell
(POSIX path of (path to temporary items from user domain)) & fileName
on error errText
""
end try
')
if [ ! -f "$tmp_file" ]; then
osascript -e'
try
tell application "Music"
tell artwork 1 of current track
set srcBytes to raw data
if format is JPEG picture then
set imgFormat to ".jpg"
else
set imgFormat to ".png"
end if
end tell
set albumName to album of current track
set albumArtist to album artist of current track
if length of albumArtist is 0
set albumArtist to artist of current track
end if
end tell
set fileName to (do shell script "echo " & quoted form of albumArtist & quoted form of albumName & " | sed \"s/[^a-zA-Z0-9]//g\"") & imgFormat
-- set fileName to (do shell script "echo " & quoted form of albumArtist & " - " & quoted form of albumName & " | sed \"s/[[:space:]]//g\"") & imgFormat
set tmpName to ((path to temporary items from user domain) as text) & fileName
set outFile to open for access file tmpName with write permission
set eof outFile to 0
write srcBytes to outFile
close access outFile
set filePath to (POSIX path of (path to temporary items from user domain)) & fileName
-- do shell script "/opt/homebrew/bin/ffmpeg -hide_banner -loglevel error -y -i " & filePath & " -vf scale=200:-1 " & filePath
do shell script "/opt/homebrew/bin/convert " & filePath & " -resize 300 -quality 95 " & filePath
end try
'
fi
if [ -f "$tmp_file" ]; then
base64img=$(base64 < "$tmp_file")
fi
if [ $state = playing ]; then
state_icon=":play:"
else
state_icon=":pause:"
fi
if [ $track != "no track selected" ]; then
echo "♫ $state_icon $track | dropdown=False length=24"
echo "♫ $state_icon $artist | dropdown=False length=36"
else
echo "♫ :stop.fill: | color=black"
fi
echo "---"
if [ $track != "no track selected" ]; then
if [ "$(osascript -l JavaScript -e 'Application("Music").shuffleEnabled()')" = "true" ]; then
shuffle_icon=":shuffle:"
else
shuffle_icon=":music.note.list:"
fi
if [ "$playlist" != "Music" ]; then
echo "$shuffle_icon From playlist: $playlist | dropdown=True length=48"
echo "$shuffle_icon $description | dropdown=True alternate=True length=50"
else
echo "♫ From album: $album | dropdown=True length=48"
fi
echo "---"
fi
if [ $state = playing ]; then
echo ":pause: Pause | bash='$0 'param1=pause terminal=False refresh=True "
echo ":backward: Previous | bash='$0' param1=previous terminal=False refresh=True "
echo ":forward: Next | bash='$0' param1=next terminal=False refresh=True "
else
echo ":play: Play | bash='$0' param1=play terminal=False refresh=True "
fi
echo "---"
if [ "$track" != "no track selected" ] && [ "$base64img" != "" ]; then
echo "| image=$base64img bash='$0' terminal=False"
fi
if [ "$track" != "no track selected" ]; then
echo "$track | length=48"
echo "$artist | length=48"
echo "$album | length=48"
fi
echo '---'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment