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>
This commit is contained in:
James Jensen 2019-11-16 05:59:20 +08:00 committed by Marc Cornellà
parent 0ad988c598
commit 451fb25703
2 changed files with 47 additions and 34 deletions

View File

@ -42,20 +42,21 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
## Commands ## Commands
| Command | Description | | Command | Description |
| :-------------- | :-------------------------------------------------- | | :-------------- | :---------------------------------------------------- |
| `tab` | Open the current directory in a new tab | | `tab` | Open the current directory in a new tab |
| `split_tab` | Split the current terminal tab horizontally | | `split_tab` | Split the current terminal tab horizontally |
| `vsplit_tab` | Split the current terminal tab vertically | | `vsplit_tab` | Split the current terminal tab vertically |
| `ofd` | Open the current directory in a Finder window | | `ofd` | Open the current directory in a Finder window |
| `pfd` | Return the path of the frontmost Finder window | | `pfd` | Return the path of the frontmost Finder window |
| `pfs` | Return the current Finder selection | | `pfs` | Return the current Finder selection |
| `cdf` | `cd` to the current Finder directory | | `cdf` | `cd` to the current Finder directory |
| `pushdf` | `pushd` to the current Finder directory | | `pushdf` | `pushd` to the current Finder directory |
| `quick-look` | Quick-Look a specified file | | `quick-look` | Quick-Look a specified file |
| `man-preview` | Open a specified man page in Preview app | | `man-preview` | Open a specified man page in Preview app |
| `showfiles` | Show hidden files | | `showfiles` | Show hidden files |
| `hidefiles` | Hide the hidden files | | `hidefiles` | Hide the hidden files |
| `itunes` | Control iTunes. Use `itunes -h` for usage details | | `itunes` | DEPRECATED. Use `music` from macOS Catalina on |
| `spotify` | Control Spotify and search by artist, album, track… | | `music` | Control Apple Music. Use `music -h` for usage details |
| `rmdsstore` | Remove .DS\_Store files recursively in a directory | | `spotify` | Control Spotify and search by artist, album, track… |
| `rmdsstore` | Remove .DS\_Store files recursively in a directory |

View File

@ -219,7 +219,19 @@ function vncviewer() {
} }
# iTunes control function # iTunes control function
function itunes() { function itunes music() {
local APP_NAME=Music
autoload is-at-least
if is-at-least 10.15 $(sw_vers -productVersion); then
if [[ $0 = itunes ]]; then
echo >&2 The itunes function name is deprecated. Use \`music\' instead.
return 1
fi
else
APP_NAME=iTunes
fi
local opt=$1 local opt=$1
local playlist=$2 local playlist=$2
shift shift
@ -236,7 +248,7 @@ function itunes() {
opt="$opt track" opt="$opt track"
;; ;;
vol) vol)
local new_volume volume=$(osascript -e 'tell application "iTunes" to get sound volume') local new_volume volume=$(osascript -e "tell application \"$APP_NAME\" to get sound volume")
if [[ $# -eq 0 ]]; then if [[ $# -eq 0 ]]; then
echo "Current volume is ${volume}." echo "Current volume is ${volume}."
return 0 return 0
@ -251,26 +263,26 @@ function itunes() {
opt="set sound volume to ${new_volume}" opt="set sound volume to ${new_volume}"
;; ;;
playlist) playlist)
# Inspired by: https://gist.github.com/nakajijapan/ac8b45371064ae98ea7f # Inspired by: https://gist.github.com/nakajijapan/ac8b45371064ae98ea7f
if [[ ! -z "$playlist" ]]; then if [[ ! -z "$playlist" ]]; then
osascript -e 'tell application "iTunes"' -e "set new_playlist to \"$playlist\" as string" -e "play playlist new_playlist" -e "end tell" 2>/dev/null; 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;
if [[ $? -eq 0 ]]; then if [[ $? -eq 0 ]]; then
opt="play" opt="play"
else else
opt="stop" opt="stop"
fi fi
else else
opt="set allPlaylists to (get name of every playlist)" opt="set allPlaylists to (get name of every playlist)"
fi fi
;; ;;
playing|status) playing|status)
local state=`osascript -e 'tell application "iTunes" to player state as string'` local state=`osascript -e "tell application \"$APP_NAME\" to player state as string"`
if [[ "$state" = "playing" ]]; then if [[ "$state" = "playing" ]]; then
currenttrack=`osascript -e 'tell application "iTunes" to name of current track as string'` currenttrack=`osascript -e "tell application \"$APP_NAME\" to name of current track as string"`
currentartist=`osascript -e 'tell application "iTunes" to artist of current track as string'` currentartist=`osascript -e "tell application \"$APP_NAME\" to artist of current track as string"`
echo -E "Listening to $fg[yellow]$currenttrack$reset_color by $fg[yellow]$currentartist$reset_color"; echo -E "Listening to $fg[yellow]$currenttrack$reset_color by $fg[yellow]$currentartist$reset_color";
else else
echo "iTunes is" $state; echo "$APP_NAME is" $state;
fi fi
return 0 return 0
;; ;;
@ -284,7 +296,7 @@ if [[ ! -z "$playlist" ]]; then
if [[ -n "$state" && ! "$state" =~ "^(on|off|toggle)$" ]] if [[ -n "$state" && ! "$state" =~ "^(on|off|toggle)$" ]]
then then
print "Usage: itunes shuffle [on|off|toggle]. Invalid option." print "Usage: $0 shuffle [on|off|toggle]. Invalid option."
return 1 return 1
fi fi
@ -305,14 +317,14 @@ EOF
esac esac
;; ;;
""|-h|--help) ""|-h|--help)
echo "Usage: itunes <option>" echo "Usage: $0 <option>"
echo "option:" echo "option:"
echo "\tlaunch|play|pause|stop|rewind|resume|quit" echo "\tlaunch|play|pause|stop|rewind|resume|quit"
echo "\tmute|unmute\tcontrol volume set" echo "\tmute|unmute\tcontrol volume set"
echo "\tnext|previous\tplay next or previous track" echo "\tnext|previous\tplay next or previous track"
echo "\tshuf|shuffle [on|off|toggle]\tSet shuffled playback. Default: toggle. Note: toggle doesn't support the MiniPlayer." echo "\tshuf|shuffle [on|off|toggle]\tSet shuffled playback. Default: toggle. Note: toggle doesn't support the MiniPlayer."
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." 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."
echo "\tplaying|status\tShow what song is currently playing in iTunes." echo "\tplaying|status\tShow what song is currently playing in Music."
echo "\tplaylist [playlist name]\t Play specific playlist" echo "\tplaylist [playlist name]\t Play specific playlist"
echo "\thelp\tshow this message and exit" echo "\thelp\tshow this message and exit"
return 0 return 0
@ -322,7 +334,7 @@ EOF
return 1 return 1
;; ;;
esac esac
osascript -e "tell application \"iTunes\" to $opt" osascript -e "tell application \"$APP_NAME\" to $opt"
} }
# Spotify control function # Spotify control function