Skip to content

Instantly share code, notes, and snippets.

@akrabat
Created July 20, 2025 15:22
Show Gist options
  • Save akrabat/8bcfac9dfef5fd4e9b67ac5bb504ea7a to your computer and use it in GitHub Desktop.
Save akrabat/8bcfac9dfef5fd4e9b67ac5bb504ea7a to your computer and use it in GitHub Desktop.
Updated Now Playing script for SwiftBar
#!/bin/bash
# <xbar.title>Now playing</xbar.title>
# <xbar.version>v1.1</xbar.version>
# <xbar.author>Adam Kenyon</xbar.author>
# <xbar.author.github>adampk90</xbar.author.github>
# <xbar.desc>Shows and controls the music that is now playing. Currently supports Spotify, iTunes, and Longplay.</xbar.desc>
# <xbar.image>https://pbs.twimg.com/media/CbKmTS7VAAA84VS.png:small</xbar.image>
# <xbar.dependencies></xbar.dependencies>
# <xbar.abouturl></xbar.abouturl>
# Modified by Rob Allen on 2021-07-02, 2025-03-06, 2025-07-20
# first, determine if there's an app that's playing or paused
# apps=(Music Spotify)
apps=(Longplay Music)
playing=""
paused=""
for i in "${apps[@]}"; do
# is the app running?
if ! app_state=$(osascript -e "application \"$i\" is running"); then
# just exit if there was an error determining the app's state
# (the app might be in the middle of quitting)
exit
fi
if [ "$app_state" = "true" ] && [ "$track" = "" ]; then
# yes, it's running
# is it playing music currently?
if [ "$i" = "Longplay" ]; then
# Longplay uses a different method to determine if it's playing
app_playing=$(shortcuts run "Longplay status");
else
# for other apps, we can use the standard method
app_playing=$(osascript -e "tell application \"$i\" to player state as string")
fi
if [ "$app_playing" = "paused" ] || [ "$app_playing" = "0" ] || [ "$app_playing" = "No" ]; then
# nope, it's paused
paused="$i"
elif [ "$app_playing" = "playing" ] || [ "$app_playing" = "1" ] || [ "$app_playing" = "Yes" ]; then
# yes, it's playing
playing="$i"
fi
fi
done
# open a specified app
if [ "$1" = "open" ]; then
osascript -e "tell application \"$2\" to activate"
exit
fi
# play/pause
if [ "$1" = "play" ] || [ "$1" = "pause" ]; then
osascript -e "tell application \"$2\" to $1"
exit
fi
# next/previous
if [ "$1" = "next" ] || [ "$1" = "previous" ]; then
osascript -e "tell application \"$2\" to $1 track"
# tell spotify to hit "Previous" twice so it actually plays the previous track
# instead of just starting from the beginning of the current one
if [ "$playing" = "Spotify" ] && [ "$1" = "previous" ]; then
osascript -e "tell application \"$2\" to $1 track"
fi
osascript -e "tell application \"$2\" to play"
exit
fi
# start outputting information to bitbar
if [ "$playing" = "" ] && [ "$paused" = "" ]; then
# nothing is even paused
echo "♫"
else
# something is playing or is paused
if [ "$playing" = "" ]; then
echo "♫"
echo "---"
echo "$paused is paused | color=#888888"
app="$paused"
else
app="$playing"
fi
if [ "$app" = "Longplay" ]; then
track=$(shortcuts run "Longplay now playing");
full_trackinfo="$track"
summary_trackinfo="${full_trackinfo:0:60}"
else
track_query="name of current track"
artist_query="artist of current track"
album_query="album of current track"
# output the track and artist
track=$(osascript -e "tell application \"$app\" to $track_query")
artist=$(osascript -e "tell application \"$app\" to $artist_query")
album=$(osascript -e "tell application \"$app\" to $album_query")
full_trackinfo="$track ~ $artist ~ $album"
# Summarise by removing anything after the first hyphen
track_summary=$(echo "$track" | awk -F ' -' '{print $1}')
artist_summary=$(echo "$artist" | awk -F ' -' '{print $1}')
album_summary=$(echo "$album" | awk -F ' -' '{print $1}')
summary_trackinfo="$track_summary ~ $artist_summary ~ $album_summary"
summary_trackinfo="${summary_trackinfo:0:60}"
fi
echo "$summary_trackinfo"
echo "---"
nowplaying="Now playing on $app"
if [ "$summary_trackinfo" != "$full_trackinfo" ]; then
nowplaying="$full_trackinfo on $app"
fi
if [ "$playing" != "" ]; then
echo "$nowplaying | bash='$0' param1=open param2=$app terminal=false"
echo "---"
echo "⏸ Pause | bash='$0' param1=pause param2=$app refresh=true terminal=false"
else
echo "▶︎ Play | bash='$0' param1=play param2=$app refresh=true terminal=false"
fi
echo "⏭ Next | bash='$0' param1=next param2=$app refresh=true terminal=false"
echo "⏮ Previous | bash='$0' param1=previous param2=$app refresh=true terminal=false"
fi
# add an Open option for each service
echo "---"
for i in "${apps[@]}"; do
echo "Open $i | bash='$0' param1=open param2=$i terminal=false"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment