Last active
December 18, 2019 23:40
-
-
Save huwr/6191039 to your computer and use it in GitHub Desktop.
Simple command line iTunes controller
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 | |
# | |
#################################### | |
# Music Command Line Control v1.0 | |
# written by David Schlosnagle | |
# created 2001.11.08 | |
# edit 2010.06.01 Rahul Kumar | |
# edit 2013.08.09 Huw Rowlands | |
# edit 2019.12.19 Huw Rowlands - Updated to Catalina (Music.app) | |
#################################### | |
showHelp() { | |
echo "-----------------------------"; | |
echo "Music Command Line Interface"; | |
echo "-----------------------------"; | |
echo "Usage: $(basename "$0") <option>"; | |
echo; | |
echo "Options:"; | |
echo " status = Shows Music's status, current artist and track."; | |
echo " pp = (play-pause) Start playing if paused, otherwise pause."; | |
echo " play = Start playing."; | |
echo " pause = Pause playing."; | |
echo " next = Go to the next track."; | |
echo " prev = (backtrack) Go to the beginning of current track or previous track if already at the start."; | |
echo " last = Go to the previous track and start playing." | |
echo " mute = Mute Music's volume."; | |
echo " unmute = Unmute Music's volume."; | |
echo " vol up = Increase Music's volume by 10%"; | |
echo " vol down = Increase Music's volume by 10%"; | |
echo " vol # = Set Music's 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"; | |
} | |
showStatus() { | |
state=$(osascript -e 'tell application "Music" to player state as string'); | |
echo "Music is $state."; | |
} | |
showPlaying() { | |
state=`osascript -e 'tell application "Music" to player state as string'`; | |
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 "$artist - $track"; | |
fi | |
} | |
if [ $# = 0 ]; then | |
showPlaying; | |
fi | |
while [ $# -gt 0 ]; do | |
arg=$1; | |
case $arg in | |
"status" ) | |
showStatus; | |
showPlaying; | |
break ;; | |
"pp" | "playpause" ) | |
osascript -e 'tell application "Music" to playpause'; | |
break ;; | |
"play" ) | |
osascript -e 'tell application "Music" to play'; | |
break ;; | |
"pause" ) | |
osascript -e 'tell application "Music" to pause'; | |
break ;; | |
"next" ) | |
osascript -e 'tell application "Music" to next track'; | |
showPlaying; | |
break ;; | |
"prev" | "previous" | "backtrack" ) | |
osascript -e 'tell application "Music" to back track'; | |
showPlaying; | |
break ;; | |
"last" ) | |
osascript -e 'tell application "Music" to previous track'; | |
showPlaying; | |
break ;; | |
"rewind" ) | |
osascript -e 'tell application "Music" to refresh'; | |
showPlaying; | |
break ;; | |
"mute" ) | |
osascript -e 'tell application "Music" to set mute to true'; | |
break ;; | |
"unmute" ) | |
osascript -e 'tell application "Music" to set mute to false'; | |
break ;; | |
"vol" ) | |
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" ) | |
osascript -e 'tell application "Music" to stop'; | |
break ;; | |
"quit" ) | |
osascript -e 'tell application "Music" to quit'; | |
break ;; | |
"albums" ) | |
osascript -e 'tell application "Music"' -e 'set allAlbums to (get album)' -e 'end tell'; | |
break;; | |
"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. (to turn off: `basename $0` nosh)"; | |
osascript -e 'tell application "Music" to set shuffle of current playlist to 1'; | |
break ;; | |
"nosh" ) echo "Shuffle is OFF. (to turn on: `basename $0` shuf)"; | |
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" | * ) | |
showHelp; | |
break ;; | |
esac | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment