451fb257032a37a7d9cae4631c03f65fd3aa7f1c

Author
James Jensen <jamesarthurjensen@gmail.com>
Committer
Marc Cornellà <marc.cornella@live.com>
Date

Message

osx: modify itunes function to support Apple Music (#8372)

Change `itunes` to `music`, as iTunes was renamed to Music in _**macOS** Catalina 10.15_

Co-authored-by: Marc Cornellà <marc.cornella@live.com>

Diff

This diff is truncated to protect this page.

  1diff --git a/plugins/osx/README.md b/plugins/osx/README.md
  2index 4595ee3195e92ffc47ed044577f2a79f1c7579e8..f3881ec6bef685dd0750d6ba4695dc6a3cf7eae0 100644
  3--- a/plugins/osx/README.md
  4+++ b/plugins/osx/README.md
  5@@ -42,20 +42,21 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  6 
  7 ## Commands
  8 
  9-| Command         | Description                                         |
 10-| :-------------- | :-------------------------------------------------- |
 11-| `tab`           | Open the current directory in a new tab             |
 12-| `split_tab`     | Split the current terminal tab horizontally         |
 13-| `vsplit_tab`    | Split the current terminal tab vertically           |
 14-| `ofd`           | Open the current directory in a Finder window       |
 15-| `pfd`           | Return the path of the frontmost Finder window      |
 16-| `pfs`           | Return the current Finder selection                 |
 17-| `cdf`           | `cd` to the current Finder directory                |
 18-| `pushdf`        | `pushd` to the current Finder directory             |
 19-| `quick-look`    | Quick-Look a specified file                         |
 20-| `man-preview`   | Open a specified man page in Preview app            |
 21-| `showfiles`     | Show hidden files                                   |
 22-| `hidefiles`     | Hide the hidden files                               |
 23-| `itunes`        | Control iTunes. Use `itunes -h` for usage details   |
 24-| `spotify`       | Control Spotify and search by artist, album, track… |
 25-| `rmdsstore`     | Remove .DS\_Store files recursively in a directory  |
 26+| Command         | Description                                           |
 27+| :-------------- | :---------------------------------------------------- |
 28+| `tab`           | Open the current directory in a new tab               |
 29+| `split_tab`     | Split the current terminal tab horizontally           |
 30+| `vsplit_tab`    | Split the current terminal tab vertically             |
 31+| `ofd`           | Open the current directory in a Finder window         |
 32+| `pfd`           | Return the path of the frontmost Finder window        |
 33+| `pfs`           | Return the current Finder selection                   |
 34+| `cdf`           | `cd` to the current Finder directory                  |
 35+| `pushdf`        | `pushd` to the current Finder directory               |
 36+| `quick-look`    | Quick-Look a specified file                           |
 37+| `man-preview`   | Open a specified man page in Preview app              |
 38+| `showfiles`     | Show hidden files                                     |
 39+| `hidefiles`     | Hide the hidden files                                 |
 40+| `itunes`        | DEPRECATED. Use `music` from macOS Catalina on        |
 41+| `music`         | Control Apple Music. Use `music -h` for usage details |
 42+| `spotify`       | Control Spotify and search by artist, album, track…   |
 43+| `rmdsstore`     | Remove .DS\_Store files recursively in a directory    |
 44diff --git a/plugins/osx/osx.plugin.zsh b/plugins/osx/osx.plugin.zsh
 45index eb3c4fb7a185b228a56688f125f02c54ee14e7d4..03e9c1c8c3a8be8d0576207280c5c543893460ed 100644
 46--- a/plugins/osx/osx.plugin.zsh
 47+++ b/plugins/osx/osx.plugin.zsh
 48@@ -219,7 +219,19 @@ function vncviewer() {
 49 }
 50 
 51 # iTunes control function
 52-function itunes() {
 53+function itunes music() {
 54+	local APP_NAME=Music
 55+
 56+	autoload is-at-least
 57+	if is-at-least 10.15 $(sw_vers -productVersion); then
 58+		if [[ $0 = itunes ]]; then
 59+			echo >&2 The itunes function name is deprecated. Use \`music\' instead.
 60+			return 1
 61+		fi
 62+	else
 63+		APP_NAME=iTunes
 64+	fi
 65+
 66 	local opt=$1
 67 	local playlist=$2
 68 	shift
 69@@ -236,7 +248,7 @@ function itunes() {
 70 			opt="$opt track"
 71 			;;
 72 		vol)
 73-			local new_volume volume=$(osascript -e 'tell application "iTunes" to get sound volume')
 74+			local new_volume volume=$(osascript -e "tell application \"$APP_NAME\" to get sound volume")
 75 			if [[ $# -eq 0 ]]; then
 76 				echo "Current volume is ${volume}."
 77 				return 0
 78@@ -251,26 +263,26 @@ function itunes() {
 79 			opt="set sound volume to ${new_volume}"
 80 			;;
 81 		playlist)
 82-		# Inspired by: https://gist.github.com/nakajijapan/ac8b45371064ae98ea7f
 83-if [[ ! -z "$playlist" ]]; then
 84-                    		osascript -e 'tell application "iTunes"' -e "set new_playlist to \"$playlist\" as string" -e "play playlist new_playlist" -e "end tell" 2>/dev/null;
 85+			# Inspired by: https://gist.github.com/nakajijapan/ac8b45371064ae98ea7f
 86+			if [[ ! -z "$playlist" ]]; then
 87+				osascript -e "tell application \"$APP_NAME\"" -e "set new_playlist to \"$playlist\" as string" -e "play playlist new_playlist" -e "end tell" 2>/dev/null;
 88 				if [[ $? -eq 0 ]]; then
 89 					opt="play"
 90 				else
 91 					opt="stop"
 92 				fi
 93-                  else
 94-                    opt="set allPlaylists to (get name of every playlist)"
 95-                  fi
 96-                ;;
 97+			else
 98+				opt="set allPlaylists to (get name of every playlist)"
 99+			fi
100+			;;
101 		playing|status)
102-			local state=`osascript -e 'tell application "iTunes" to player state as string'`
103+			local state=`osascript -e "tell application \"$APP_NAME\" to player state as string"`
104 			if [[ "$state" = "playing" ]]; then
105-				currenttrack=`osascript -e 'tell application "iTunes" to name of current track as string'`
106-				currentartist=`osascript -e 'tell application "iTunes" to artist of current track as string'`
107+				currenttrack=`osascript -e "tell application \"$APP_NAME\" to name of current track as string"`
108+				currentartist=`osascript -e "tell application \"$APP_NAME\" to artist of current track as string"`
109 				echo -E "Listening to $fg[yellow]$currenttrack$reset_color by $fg[yellow]$currentartist$reset_color";
110 			else
111-				echo "iTunes is" $state;
112+				echo "$APP_NAME is" $state;
113 			fi
114 			return 0
115 			;;
116@@ -284,7 +296,7 @@ if [[ ! -z "$playlist" ]]; then
117 
118 			if [[ -n "$state" && ! "$state" =~ "^(on|off|toggle)$" ]]
119 			then
120-				print "Usage: itunes shuffle [on|off|toggle]. Invalid option."
121+				print "Usage: $0 shuffle [on|off|toggle]. Invalid option."
122 				return 1
123 			fi
124 
125@@ -305,14 +317,14 @@ EOF
126 			esac
127 			;;
128 		""|-h|--help)
129-			echo "Usage: itunes <option>"
130+			echo "Usage: $0 <option>"
131 			echo "option:"
132 			echo "\tlaunch|play|pause|stop|rewind|resume|quit"
133 			echo "\tmute|unmute\tcontrol volume set"
134 			echo "\tnext|previous\tplay next or previous track"
135 			echo "\tshuf|shuffle [on|off|toggle]\tSet shuffled playback. Default: toggle. Note: toggle doesn't support the MiniPlayer."
136 			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."
137-			echo "\tplaying|status\tShow what song is currently playing in iTunes."
138+			echo "\tplaying|status\tShow what song is currently playing in Music."
139 			echo "\tplaylist [playlist name]\t Play specific playlist"
140 			echo "\thelp\tshow this message and exit"
141 			return 0
142@@ -322,7 +334,7 @@ EOF
143 			return 1
144 			;;
145 	esac
146-	osascript -e "tell application \"iTunes\" to $opt"
147+	osascript -e "tell application \"$APP_NAME\" to $opt"