Created
December 6, 2013 22:19
-
-
Save mateuszgachowski-snippets/7833114 to your computer and use it in GitHub Desktop.
Bash: iTunes CLI
This file contains hidden or 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/sh | |
| # | |
| #################################### | |
| # iTunes Command Line Control v1.0 | |
| # written by David Schlosnagle | |
| # created 2001.11.08 | |
| #################################### | |
| showHelp () { | |
| echo "-----------------------------"; | |
| echo "iTunes Command Line Interface"; | |
| echo "-----------------------------"; | |
| echo "Usage: `basename $0` <option>"; | |
| echo; | |
| echo "Options:"; | |
| echo " status = Shows iTunes' status, current artist and track."; | |
| echo " play = Start playing iTunes."; | |
| echo " pause = Pause iTunes."; | |
| echo " next = Go to the next track."; | |
| echo " prev = Go to the previous track."; | |
| echo " mute = Mute iTunes' volume."; | |
| echo " unmute = Unmute iTunes' volume."; | |
| echo " vol up = Increase iTunes' volume by 10%"; | |
| echo " vol down = Increase iTunes' volume by 10%"; | |
| echo " vol # = Set iTunes' volume to # [0-100]"; | |
| echo " stop = Stop iTunes."; | |
| echo " quit = Quit iTunes."; | |
| } | |
| if [ $# = 0 ]; then | |
| showHelp; | |
| fi | |
| while [ $# -gt 0 ]; do | |
| arg=$1; | |
| case $arg in | |
| "status" ) state=`osascript -e 'tell application "iTunes" to player state as string'`; | |
| echo "iTunes is currently $state."; | |
| if [ $state = "playing" ]; then | |
| artist=`osascript -e 'tell application "iTunes" to artist of current track as string'`; | |
| track=`osascript -e 'tell application "iTunes" to name of current track as string'`; | |
| echo "Current track $artist: $track"; | |
| fi | |
| break ;; | |
| 'start' ) echo 'Launching iTunes' | |
| osascript -e 'tell application "iTunes" to launch'; | |
| break;; | |
| "play" ) echo "Playing iTunes."; | |
| if [ $# -eq 2 ]; then | |
| track_id=$2 | |
| echo 'Play track with ID' $track_id | |
| osascript -e "tell application \"iTunes\" to play ((first track of current playlist) whose database ID is $track_id)" | |
| else | |
| osascript -e 'tell application "iTunes" to play'; | |
| fi | |
| break ;; | |
| "pause" ) echo "Pausing iTunes."; | |
| osascript -e 'tell application "iTunes" to pause'; | |
| break ;; | |
| "next" ) echo "Going to next track." ; | |
| osascript -e 'tell application "iTunes" to next track'; | |
| break ;; | |
| "prev" ) echo "Going to previous track."; | |
| osascript -e 'tell application "iTunes" to previous track'; | |
| break ;; | |
| "mute" ) echo "Muting iTunes volume level."; | |
| osascript -e 'tell application "iTunes" to set mute to true'; | |
| break ;; | |
| "unmute" ) echo "Unmuting iTunes volume level."; | |
| osascript -e 'tell application "iTunes" to set mute to false'; | |
| break ;; | |
| "vol" ) echo "Changing iTunes volume level."; | |
| vol=`osascript -e 'tell application "iTunes" to sound volume as integer'`; | |
| if [ $2 = "up" ]; then | |
| newvol=$(( vol+10 )); | |
| fi | |
| if [ $2 = "down" ]; then | |
| newvol=$(( vol-10 )); | |
| fi | |
| if [ $2 -gt 0 ]; then | |
| newvol=$2; | |
| fi | |
| osascript -e "tell application \"iTunes\" to set sound volume to $newvol"; | |
| break ;; | |
| "album" ) | |
| id=`osascript -e "tell application \"iTunes\" to (get database ID of every track in current playlist)"` | |
| album=`osascript -e "tell application \"iTunes\" to (get album of every track in current playlist)"` | |
| #split IDs based on ',' | |
| export IFS="," | |
| #get database ID of tracks | |
| i=0 | |
| for id in $id | |
| do | |
| id_a[$i]=`echo $id` | |
| i=$((i+1)) | |
| done | |
| echo "Total number of songs in current playlist:" ${#id_a[@]} | |
| echo "\tFor every album in DB, display album name and first track ID of album." | |
| #disable separator | |
| export IFS="" | |
| ALBUM=( ) | |
| for (( i = 0 ; i < ${#id_a[@]} ; i++ )) | |
| do | |
| NEW_ALBUM=`osascript -e "tell application \"iTunes\" to get album of (every track where database ID is ${id_a[$i]})"` | |
| if [ ${#ALBUM[@]} -eq 0 ]; then | |
| ALBUM=( "${ALBUM[@]}" "$NEW_ALBUM" ) | |
| else | |
| MARK=1 | |
| for j in ${ALBUM[@]}; do | |
| if [[ $j == "$NEW_ALBUM" ]] ; then | |
| MARK=0 | |
| break | |
| fi | |
| done | |
| if [ $MARK -ne 0 ]; then | |
| ALBUM=( "${ALBUM[@]}" "$NEW_ALBUM" ) | |
| echo ${id_a[$i]} $NEW_ALBUM | |
| fi | |
| fi | |
| done | |
| break;; | |
| "stop" ) echo "Stopping iTunes."; | |
| osascript -e 'tell application "iTunes" to stop'; | |
| break ;; | |
| "quit" ) echo "Quitting iTunes."; | |
| osascript -e 'tell application "iTunes" to quit'; | |
| exit 1 ;; | |
| "help" | * ) echo "help:"; | |
| showHelp; | |
| break ;; | |
| esac | |
| done |
Cool script, but I'm always getting a "Permission Denied" message with any of the basic itunes commands. Using macOS 10.14.6. Maybe a noob issue, but any help would be greatly appreciated!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Neat script!
I find the command name
searchweird, when it actually searches AND plays the matched albums.