Created
September 9, 2024 20:15
-
-
Save rogeruiz/817a92a4919db531baf1c184f842516b to your computer and use it in GitHub Desktop.
This is an example script that uses `nowplaying-cli` to extract things without needing `jq`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
generic_prefix='kMRMediaRemoteNowPlayingInfo' | |
apple_prefix='MRMediaRemoteMediaType' | |
# Requires Bash 4+ due to associative array usage in this script. | |
declare -A npc_response=( | |
[artist]='null' | |
[title]='null' | |
[album]='null' | |
[mediaType]='null' | |
[playbackRate]='null' | |
) | |
count=1 | |
_run() { | |
nowplaying-cli get artist title album mediaType playbackRate | { | |
while read -r line; do | |
case ${count} in | |
1) npc_response[artist]="$line" ;; | |
2) npc_response[title]="$line" ;; | |
3) npc_response[album]="$line" ;; | |
4) | |
case ${line} in | |
"${generic_prefix}TypeAudio") npc_response[mediaType]="<GENERAL_AUDIO_ICON>|<SPOTIFY_ICON>" ;; | |
"${apple_prefix}Music") npc_response[mediaType]="<APPLE_MUSIC_ICON>" ;; | |
"${apple_prefix}Podcast") npc_response[mediaType]="<PODCAST_ICON>" ;; | |
null) npc_response[mediaType]="<MISC_AUDIO_ICON>|<BROWSER_ICON>" ;; | |
*) ;; | |
esac | |
;; | |
5) npc_response[playbackRate]="$line" ;; | |
*) ;; | |
esac | |
count=$((count + 1)) | |
done | |
if [[ "${npc_response[playbackRate]}" == "1" ]]; then | |
ICON+="<PLAYING_ICON> " | |
elif [[ "${npc_response[playbackRate]}" == "0" ]]; then | |
ICON+="<PAUSED_ICON> " | |
fi | |
ICON+="${npc_response[mediaType]} " | |
if [[ "${npc_response[artist]}" != "null" ]]; then | |
LABEL+="${npc_response[artist]} / " | |
fi | |
if [[ "${npc_response[title]}" != "null" ]]; then | |
LABEL+="${npc_response[title]}" | |
fi | |
if [[ "${npc_response[album]}" != "null" ]]; then | |
LABEL+=" / ${npc_response[album]}" | |
fi | |
echo "${ICON} ${LABEL}" | |
} | |
} | |
_run "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment