Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
control iTunes from command line
#!/bin/bash
#
####################################
# iTunes Command Line Control v1.0
# written by David Schlosnagle
# created 2001.11.08
# edit 2010.06.01 rahul kumar
####################################
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.";
echo " playlist = Show playlists saved in iTunes.";
echo " tracks = Show tracks for current or given playlist.";
echo " shuf = Shuffle current playlist";
echo " nosh = Do not shuffle current playlist";
}
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 ;;
"play" ) echo "Playing iTunes.";
osascript -e 'tell application "iTunes" to play';
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 ));
elif [ $2 = "down" ]; then
newvol=$(( vol-10 ));
elif [ $2 -gt 0 ]; then
newvol=$2;
fi
osascript -e "tell application \"iTunes\" to set sound volume to $newvol";
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 ;;
## addition playlist of choice
"playlist" )
if [ -n "$2" ]; then
echo "Changing iTunes playlists to '$2'.";
osascript -e 'tell application "iTunes"' -e "set new_playlist to \"$2\" as string" -e "play playlist new_playlist" -e "end tell";
break ;
else
# Show available iTunes playlists.
echo "Playlists:";
osascript -e 'tell application "iTunes"' -e "set allPlaylists to (get name of every playlist)" -e "end tell";
break;
fi
break;;
"shuf" ) echo "Shuffle is ON.";
osascript -e 'tell application "iTunes" to set shuffle of current playlist to 1';
break ;;
"nosh" ) echo "Shuffle is OFF.";
osascript -e 'tell application "iTunes" to set shuffle of current playlist to 0';
break ;;
"tracks" )
if [ -n "$2" ]; then
osascript -e 'tell application "iTunes"' -e "set new_playlist to \"$2\" as string" -e " get name of every track in playlist new_playlist" -e "end tell";
break;
fi
osascript -e 'tell application "iTunes" to get name of every track in current playlist';
break ;;
"help" | * ) echo "help:";
showHelp;
break ;;
esac
done
@genuinefafa

This comment has been minimized.

Copy link

@genuinefafa genuinefafa commented Sep 20, 2017

Nice! What about scripting on Windows?

@ghost

This comment has been minimized.

Copy link

@ghost ghost commented Jan 6, 2018

@genuinefafa
It's possible to partially use iTunes from Windows using Powershell - Here's how:
Config:

  • OS: Windows 10 - 64 bits - Fall Creators Update
  • iTunes version: 12.7
  • Powershell version: 5.1

In Powershell:

# search for iTunes COM object
# one result will be iTunes.Application.1                      iTunes Class
Get-CimInstance Win32_COMSetting|Select-Object ProgId, Caption|Where-Object Caption -ILike "*itunes*"

# instanciate the object - the .<number> at the end may reference a version
$itunes = New-Object -ComObject iTunes.Application

# play any file from your hard drive
$itunes.PlayFile "C:\Users\<username>\some_music_file.mp>"

# change the volume by modifying the SoundVolume property of the $iTunes object where 0 is mute and 100 max volume
$itunes.SoundVolume = 50

# get all properties/methods of the iTunes COM object - Parameter types will be displayed and wether it only returns
# a value (get;), sets one (set;) or does both (get;set;) 
$iTunes|Get-Member

In fact - these steps work in the same way to for instance run Excel and perform any operations like creating a Macro, showing or hidding the application and so on.

@beninato8

This comment has been minimized.

Copy link

@beninato8 beninato8 commented May 29, 2018

For High Sierra with iTunes 12.7, shuffling can be toggled using this: tell application "iTunes" to set shuffle enabled to True

@LinZap

This comment has been minimized.

Copy link

@LinZap LinZap commented Sep 24, 2018

@genuinefafa
It's possible to partially use iTunes from Windows using Powershell - Here's how:
Config:

  • OS: Windows 10 - 64 bits - Fall Creators Update
  • iTunes version: 12.7
  • Powershell version: 5.1

In Powershell:

# search for iTunes COM object
# one result will be iTunes.Application.1                      iTunes Class
Get-CimInstance Win32_COMSetting|Select-Object ProgId, Caption|Where-Object Caption -ILike "*itunes*"

# instanciate the object - the .<number> at the end may reference a version
$itunes = New-Object -ComObject iTunes.Application

# play any file from your hard drive
$itunes.PlayFile "C:\Users\<username>\some_music_file.mp>"

# change the volume by modifying the SoundVolume property of the $iTunes object where 0 is mute and 100 max volume
$itunes.SoundVolume = 50

# get all properties/methods of the iTunes COM object - Parameter types will be displayed and wether it only returns
# a value (get;), sets one (set;) or does both (get;set;) 
$iTunes|Get-Member

In fact - these steps work in the same way to for instance run Excel and perform any operations like creating a Macro, showing or hidding the application and so on.

Beautiful! This worked!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.