music 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #!/usr/bin/env zsh
  2. function music itunes() {
  3. local APP_NAME=Music sw_vers=$(sw_vers -productVersion 2>/dev/null)
  4. autoload is-at-least
  5. if [[ -z "$sw_vers" ]] || is-at-least 10.15 $sw_vers; then
  6. if [[ $0 = itunes ]]; then
  7. echo >&2 The itunes function name is deprecated. Use \'music\' instead.
  8. return 1
  9. fi
  10. else
  11. APP_NAME=iTunes
  12. fi
  13. local opt=$1 playlist=$2
  14. (( $# > 0 )) && shift
  15. case "$opt" in
  16. launch|play|pause|stop|rewind|resume|quit)
  17. ;;
  18. mute)
  19. opt="set mute to true"
  20. ;;
  21. unmute)
  22. opt="set mute to false"
  23. ;;
  24. next|previous)
  25. opt="$opt track"
  26. ;;
  27. vol)
  28. local new_volume volume=$(osascript -e "tell application \"$APP_NAME\" to get sound volume")
  29. if [[ $# -eq 0 ]]; then
  30. echo "Current volume is ${volume}."
  31. return 0
  32. fi
  33. case $1 in
  34. up) new_volume=$((volume + 10 < 100 ? volume + 10 : 100)) ;;
  35. down) new_volume=$((volume - 10 > 0 ? volume - 10 : 0)) ;;
  36. <0-100>) new_volume=$1 ;;
  37. *) echo "'$1' is not valid. Expected <0-100>, up or down."
  38. return 1 ;;
  39. esac
  40. opt="set sound volume to ${new_volume}"
  41. ;;
  42. playlist)
  43. # Inspired by: https://gist.github.com/nakajijapan/ac8b45371064ae98ea7f
  44. if [[ -n "$playlist" ]]; then
  45. osascript 2>/dev/null <<EOF
  46. tell application "$APP_NAME"
  47. set new_playlist to "$playlist" as string
  48. play playlist new_playlist
  49. end tell
  50. EOF
  51. if [[ $? -eq 0 ]]; then
  52. opt="play"
  53. else
  54. opt="stop"
  55. fi
  56. else
  57. opt="set allPlaylists to (get name of every playlist)"
  58. fi
  59. ;;
  60. playing|status)
  61. local currenttrack currentartist state=$(osascript -e "tell application \"$APP_NAME\" to player state as string")
  62. if [[ "$state" = "playing" ]]; then
  63. currenttrack=$(osascript -e "tell application \"$APP_NAME\" to name of current track as string")
  64. currentartist=$(osascript -e "tell application \"$APP_NAME\" to artist of current track as string")
  65. echo -E "Listening to ${fg[yellow]}${currenttrack}${reset_color} by ${fg[yellow]}${currentartist}${reset_color}"
  66. else
  67. echo "$APP_NAME is $state"
  68. fi
  69. return 0
  70. ;;
  71. shuf|shuff|shuffle)
  72. # The shuffle property of current playlist can't be changed in iTunes 12,
  73. # so this workaround uses AppleScript to simulate user input instead.
  74. # Defaults to toggling when no options are given.
  75. # The toggle option depends on the shuffle button being visible in the Now playing area.
  76. # On and off use the menu bar items.
  77. local state=$1
  78. if [[ -n "$state" && "$state" != (on|off|toggle) ]]; then
  79. print "Usage: $0 shuffle [on|off|toggle]. Invalid option."
  80. return 1
  81. fi
  82. case "$state" in
  83. on|off)
  84. # Inspired by: https://stackoverflow.com/a/14675583
  85. osascript >/dev/null 2>&1 <<EOF
  86. tell application "System Events" to perform action "AXPress" of (menu item "${state}" of menu "Shuffle" of menu item "Shuffle" of menu "Controls" of menu bar item "Controls" of menu bar 1 of application process "iTunes" )
  87. EOF
  88. return 0
  89. ;;
  90. toggle|*)
  91. osascript >/dev/null 2>&1 <<EOF
  92. tell application "System Events" to perform action "AXPress" of (button 2 of process "iTunes"'s window "iTunes"'s scroll area 1)
  93. EOF
  94. return 0
  95. ;;
  96. esac
  97. ;;
  98. ""|-h|--help)
  99. echo "Usage: $0 <option>"
  100. echo "option:"
  101. echo "\t-h|--help\tShow this message and exit"
  102. echo "\tlaunch|play|pause|stop|rewind|resume|quit"
  103. echo "\tmute|unmute\tMute or unmute $APP_NAME"
  104. echo "\tnext|previous\tPlay next or previous track"
  105. echo "\tshuf|shuffle [on|off|toggle]\tSet shuffled playback. Default: toggle. Note: toggle doesn't support the MiniPlayer."
  106. echo "\tvol [0-100|up|down]\tGet or set the volume. 0 to 100 sets the volume. 'up' / 'down' increases / decreases by 10 points. No argument displays current volume."
  107. echo "\tplaying|status\tShow what song is currently playing in Music."
  108. echo "\tplaylist [playlist name]\t Play specific playlist"
  109. return 0
  110. ;;
  111. *)
  112. print "Unknown option: $opt"
  113. return 1
  114. ;;
  115. esac
  116. osascript -e "tell application \"$APP_NAME\" to $opt"
  117. }
  118. function _music() {
  119. local app_name
  120. case "$words[1]" in
  121. itunes) app_name="iTunes" ;;
  122. music|*) app_name="Music" ;;
  123. esac
  124. local -a cmds subcmds
  125. cmds=(
  126. "launch:Launch the ${app_name} app"
  127. "play:Play ${app_name}"
  128. "pause:Pause ${app_name}"
  129. "stop:Stop ${app_name}"
  130. "rewind:Rewind ${app_name}"
  131. "resume:Resume ${app_name}"
  132. "quit:Quit ${app_name}"
  133. "mute:Mute the ${app_name} app"
  134. "unmute:Unmute the ${app_name} app"
  135. "next:Skip to the next song"
  136. "previous:Skip to the previous song"
  137. "vol:Change the volume"
  138. "playlist:Play a specific playlist"
  139. {playing,status}":Show what song is currently playing"
  140. {shuf,shuff,shuffle}":Set shuffle mode"
  141. {-h,--help}":Show usage"
  142. )
  143. if (( CURRENT == 2 )); then
  144. _describe 'command' cmds
  145. elif (( CURRENT == 3 )); then
  146. case "$words[2]" in
  147. vol) subcmds=( 'up:Raise the volume' 'down:Lower the volume' )
  148. _describe 'command' subcmds ;;
  149. shuf|shuff|shuffle) subcmds=('on:Switch on shuffle mode' 'off:Switch off shuffle mode' 'toggle:Toggle shuffle mode (default)')
  150. _describe 'command' subcmds ;;
  151. esac
  152. elif (( CURRENT == 4 )); then
  153. case "$words[2]" in
  154. playlist) subcmds=('play:Play the playlist (default)' 'stop:Stop the playlist')
  155. _describe 'command' subcmds ;;
  156. esac
  157. fi
  158. return 0
  159. }
  160. compdef _music music itunes