Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save albertomorini/10a1f7403b08d572b44ca96597f11d86 to your computer and use it in GitHub Desktop.
Save albertomorini/10a1f7403b08d572b44ca96597f11d86 to your computer and use it in GitHub Desktop.
iTunes Bash Controller
#!/bin/bash
#
####################################
# Music Command Line Control v1.1
# Modified by Alberto Morini on 2024/07/10 replacing 'iTunes' to 'Music' thust to work with newer MacOS
# written by David Schlosnagle
# created 2001.11.08
# edit 2010.06.01 rahul kumar
####################################
showHelp () {
echo "-----------------------------";
echo "Music Command Line Interface";
echo "-----------------------------";
echo "Usage: `basename $0` <option>";
echo;
echo "Options:";
echo " status = Shows Music' status, current artist and track.";
echo " play = Start playing Music.";
echo " pause = Pause Music.";
echo " next = Go to the next track.";
echo " prev = Go to the previous track.";
echo " mute = Mute Music' volume.";
echo " unmute = Unmute Music' volume.";
echo " vol up = Increase Music' volume by 10%";
echo " vol down = Increase Music' volume by 10%";
echo " vol # = Set Music' volume to # [0-100]";
echo " stop = Stop Music.";
echo " quit = Quit Music.";
echo " playlist = Show playlists saved in Music.";
echo " tracks = Show tracks for current or given playlist.";
echo " shuf = Shuffle current playlist";
echo " nosh = Do not shuffle current playlist";
}
if [ $# = 0 ]; then
showHelp;
fi
while [ $# -gt 0 ]; do
arg=$1;
case $arg in
"status" ) state=`osascript -e 'tell application "Music" to player state as string'`;
echo "Music is currently $state.";
if [ $state = "playing" ]; then
artist=`osascript -e 'tell application "Music" to artist of current track as string'`;
track=`osascript -e 'tell application "Music" to name of current track as string'`;
echo "Current track $artist: $track";
fi
break ;;
"play" ) echo "Playing Music.";
osascript -e 'tell application "Music" to play';
break ;;
"pause" ) echo "Pausing Music.";
osascript -e 'tell application "Music" to pause';
break ;;
"next" ) echo "Going to next track." ;
osascript -e 'tell application "Music" to next track';
break ;;
"prev" ) echo "Going to previous track.";
osascript -e 'tell application "Music" to previous track';
break ;;
"mute" ) echo "Muting Music volume level.";
osascript -e 'tell application "Music" to set mute to true';
break ;;
"unmute" ) echo "Unmuting Music volume level.";
osascript -e 'tell application "Music" to set mute to false';
break ;;
"vol" ) echo "Changing Music volume level.";
vol=`osascript -e 'tell application "Music" to sound volume as integer'`;
if [ $2 = "up" ]; then
newvol=$(( vol+10 ));
elif [ $2 = "down" ]; then
newvol=$(( vol-10 ));
elif [ $2 -gt 0 ]; then
newvol=$2;
fi
osascript -e "tell application \"Music\" to set sound volume to $newvol";
break ;;
"stop" ) echo "Stopping Music.";
osascript -e 'tell application "Music" to stop';
break ;;
"quit" ) echo "Quitting Music.";
osascript -e 'tell application "Music" to quit';
exit 1 ;;
## addition playlist of choice
"playlist" )
if [ -n "$2" ]; then
echo "Changing Music playlists to '$2'.";
osascript -e 'tell application "Music"' -e "set new_playlist to \"$2\" as string" -e "play playlist new_playlist" -e "end tell";
break ;
else
# Show available Music playlists.
echo "Playlists:";
osascript -e 'tell application "Music"' -e "set allPlaylists to (get name of every playlist)" -e "end tell";
break;
fi
break;;
"shuf" ) echo "Shuffle is ON.";
osascript -e 'tell application "Music" to set shuffle of current playlist to 1';
break ;;
"nosh" ) echo "Shuffle is OFF.";
osascript -e 'tell application "Music" to set shuffle of current playlist to 0';
break ;;
"tracks" )
if [ -n "$2" ]; then
osascript -e 'tell application "Music"' -e "set new_playlist to \"$2\" as string" -e " get name of every track in playlist new_playlist" -e "end tell";
break;
fi
osascript -e 'tell application "Music" to get name of every track in current playlist';
break ;;
"help" | * ) echo "help:";
showHelp;
break ;;
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment