Accessing Longplay info for SwiftBar
One app that I find incredibly useful is SwiftBar and one use I have is to display track info for the currently playing song in Apple Music.
SwiftBar plugins work as shell scripts that execute on a timer and echo specially formatted text which SwiftBar then turns into an item on the menu bar with an attached menu
I use a heavily modified Now Playing plugin that was originally written by Adam Kenyon, so all the hard work was done by them.
Recently, I’ve been using Longplay to play albums and wanted the same functionality.
Now Playing uses AppleScript to determine if a music player is playing and what the track info is:
app_playing=$(osascript -e "tell application \"$i\" to player state as string")
And
track=$(osascript -e "tell application \"$app\" to name of current track") artist=$(osascript -e "tell application \"$app\" to artist of current track")
When looking at adding Longplay, I was pleased to discover that it has AppleScript support, but perusing the Dictionary, I discovered that it doesn’t support the features I need here.
Upon emailing the developer, they very helpfully pointed out that Longplay also has Shortcuts support and that I could probably use that instead. They were right.
I knocked up a couple of shortcuts:

and

With these set-up, I can now run them from the command line using shortcuts:
app_playing=$(shortcuts run "Longplay status");
This will set $app_playing to either “Yes” or “No” as strings as it is defined as boolean in Shortcuts.
track=$(shortcuts run "Longplay now playing");
This simply sets $track to the string of the currently playing track.
Updated Now Playing script
With the ability to get the info I needed from the command line, I hacked updated my copy of the Now Playing script and all is good.
I’ve updated it a bit over the years, so I’ve uploaded my version to Gist: nowplaying.5s.sh.