Add itunes shuffle command to OSX plugin.
Added shuffle functionality to the itunes command in the OSX plugin.
iTunes 12 doesn't support setting the "shuffle" property on the playlist
from what I've read, so this simulates the user interacting with the
iTunes interface. The toggle option "clicks" the shuffle button in the
Now Playing area. This has the effect of toggling the shuffle setting on
whatever is currently playing. The on and off options use the Shuffle
menu bar items.
Usage: itunes shuffle [on|off|toggle]
Diff
1diff --git a/plugins/osx/osx.plugin.zsh b/plugins/osx/osx.plugin.zsh
2index 2eea0d8ef1b5de801dd43a03b86d964e30cd2113..d366f1fc879f19c060e3a7606dafaa96133ad34e 100644
3--- a/plugins/osx/osx.plugin.zsh
4+++ b/plugins/osx/osx.plugin.zsh
5@@ -177,12 +177,43 @@ function itunes() {
6 vol)
7 opt="set sound volume to $1" #$1 Due to the shift
8 ;;
9+ shuf|shuff|shuffle)
10+ # The shuffle property of current playlist can't be changed in iTunes 12,
11+ # so this workaround uses AppleScript to simulate user input instead.
12+ # Defaults to toggling when no options are given.
13+ # The toggle option depends on the shuffle button being visible in the Now playing area.
14+ # On and off use the menu bar items.
15+ local state=$1
16+
17+ if [[ -n "$state" && ! "$state" =~ "^(on|off|toggle)$" ]]
18+ then
19+ print "Usage: itunes shuffle [on|off|toggle]. Invalid option."
20+ return 1
21+ fi
22+
23+ case "$state" in
24+ on|off)
25+ # Inspired by: http://stackoverflow.com/a/14675583
26+ osascript 1>/dev/null 2>&1 <<-EOF
27+ 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" )
28+EOF
29+ return 0
30+ ;;
31+ toggle|*)
32+ osascript 1>/dev/null 2>&1 <<-EOF
33+ tell application "System Events" to perform action "AXPress" of (button 2 of process "iTunes"'s window "iTunes"'s scroll area 1)
34+EOF
35+ return 0
36+ ;;
37+ esac
38+ ;;
39 ""|-h|--help)
40 echo "Usage: itunes <option>"
41 echo "option:"
42 echo "\tlaunch|play|pause|stop|rewind|resume|quit"
43 echo "\tmute|unmute\tcontrol volume set"
44 echo "\tnext|previous\tplay next or previous track"
45+ echo "\tshuf|shuffle [on|off|toggle]\tSet shuffled playback. Default: toggle. Note: toggle doesn't support the MiniPlayer."
46 echo "\tvol\tSet the volume, takes an argument from 0 to 100"
47 echo "\thelp\tshow this message and exit"
48 return 0