#!/usr/bin/python
###################################################
#
# iTunes Python Shell Script v0.1
# inspired by the bash shell scripts on MacOSXHints
# http://www.macosxhints.com/article.php?story=20011108211802830
#
# README
# - give read/execute permissions to the file (ex 'chmod 755 ./itunes')
# - import this script into your bin director (ex '/usr/bin' or '~/bin')
from os import system as cmd
from sys import argv as args
def myException(x):
print x
raise SystemExit
def defHelp():
myException(" No arguments provided - Usage %s \n for a list of commands: %s -h"%(args[0],args[0]))
def showHelp():
helpHeader = "---------------------------------------------------"
print helpHeader+"\n iTunes Command Line Interface\n"+helpHeader
print "Usage: %s \n"%args[0]
print """Options:
status = Shows iTunes' status, current artist, and track.
play = Start playing iTunes.
stop = Stop iTunes
pause = Pause iTunes.
playpause = Play/Pause iTunes.
next = Go to the next track.
prev = Go to the previous track.
mute = Mute iTunes' volume.
unmute = Unmute iTunes' volume.
vol up = Increase iTunes' volume by 10%
vol down = Increase iTunes' volume by 10%
vol # = Set iTunes' volume to # [0-100]
quit = Quit iTunes
launch = Launch iTunes
playlist @ = Play iTunes' playlist named @"
list = list playlists.
shuf = turn on shuffle playlist
noshuf = turn off shuffle playlist
ostream = open stream
help = what's showing now"""
myException(helpHeader)
def play():
print "Playing iTunes"
cmd('osascript -e \'tell application "iTunes" to play\'')
def stop():
print "Stopping iTunes"
cmd('osascript -e \'tell application "iTunes" to stop\'')
def pause():
print "Pausing Itunes"
cmd('osascript -e \'tell application "iTunes" to pause\'')
def playpause():
print "Play/Pause Itunes"
cmd('osascript -e \'tell application "iTunes" to playpause\'')
def next():
print "Going to the next track"
cmd('osascript -e \'tell application "iTunes" to next track\'')
def prev():
print "Going to the previous track"
cmd('osascript -e \'tell application "iTunes" to previous track\'')
def mute():
print "Muting iTunes volume level."
cmd('osascript -e \'tell application "iTunes" to set mute to true\'')
def unmute():
print "Unmuting iTunes volume level."
cmd('osascript -e \'tell application "iTunes" to set mute to false\'')
def quit():
print "Quitting iTunes"
cmd('osascript -e \'tell application "iTunes" to quit\'')
def launch():
print "Launching iTunes"
cmd('osascript -e \'tell application "iTunes" to launch\'')
def shuf():
print "Shuffling current playlist"
cmd('osascript -e \'tell application "iTunes" to set shuffle of current playlist to 1\'')
def noshuf():
print "No longer shuffling current playlist"
cmd('osascript -e \'tell application "iTunes" to set shuffle of current playlist to 0\'')
def ostream():
try:
print "Playing playlist %s"%(args[2])
#cmd('osascript -e \'tell application "iTunes"\' -e "open location \"%s\"" -e "end tell"\''%(args[2]))
cmd('osascript -e \'tell application "iTunes"\' -e \'open location "%s"\' -e \'end tell\''%(args[2]))
except:
myException("Please provide the stream location you want to play")
def status():
cmd('osascript -e \'tell application "iTunes" to player state as string\' > /tmp/iTuneState')
f = open('/tmp/iTuneState', 'r')
s = f.readline().strip()
print "iTunes is currently %s"%(s)
cmd('rm /tmp/iTuneState')
shufstat()
if s == "playing":
trackstat()
def trackstat():
cmd('osascript -e \'tell application "iTunes" to name of current playlist as string\' > /tmp/iTunesPlaylist')
cmd('osascript -e \'tell application "iTunes" to artist of current track as string\' >> /tmp/iTunesPlaylist')
cmd('osascript -e \'tell application "iTunes" to name of current track as string\' >> /tmp/iTunesPlaylist')
f = open('/tmp/iTunesPlaylist', 'r')
s = f.read()
x,y,z,a = s.split('\n')
print "Current track %s: %s, on playlist \"%s\""%(y,z,x)
cmd('rm /tmp/iTunesPlaylist')
def shufstat():
cmd("osascript -e 'tell application \"iTunes\" to get shuffle of current playlist' > /tmp/iTunesShuffle")
f = open('/tmp/iTunesShuffle', 'r')
s = f.readline().strip()
print "Shuffling: %s"%(s)
cmd('rm /tmp/iTunesShuffle')
def vol():
print "Changing iTunes volume level."
cmd('osascript -e \'tell application "iTunes" to get (sound volume as integer)\' > /tmp/iTunesVol')
f = open('/tmp/iTunesVol')
s = int(f.readline().strip())
try: args[2]
except: myException("could not understand your volume command\n'up', 'down', or integer value (ie. '60')")
if args[2] == "up":
newvol = s+10
elif args[2] == "down":
newvol = s-10
else:
try:
newvol = int(args[2])
except:
myException("could not understand your volume command\n'up', 'down', or integer value (ie. '60')")
cmd('osascript -e \'tell application "iTunes" to set sound volume to (%i as integer)\''%(newvol))
print "from %i to %i"%(s,newvol)
cmd('rm /tmp/iTunesVol')
def playlist():
try:
print "Changing iTunes plalist to %s"%(args[2])
cmd('osascript -e \'tell application "iTunes"\' -e \'set the new_playlist to "%s" as string\' -e "play playlist new_playlist" -e "end tell"'%(args[2]))
except:
myException("Pleasae define the playlist you want to play")
def llist():
print "Listing available playlists:"
cmd('osascript -e \'tell application "iTunes"\' -e \'name of every user playlist of source "Library"\' -e "end tell" > /tmp/iTunesPlaylists')
f = open('/tmp/iTunesPlaylists')
s = f.read()
s = s.replace("\n","")
print s
cmd('rm /tmp/iTunesPlaylists')
# if no arguments then show help and exit
try: args[1]
except: defHelp()#showHelp()
# switch/case like statement
action = {
"status": status, "play": play, "pause": pause, "playpause": playpause,
"next": next, "prev": prev, "mute": mute, "unmute": unmute,
"vol": vol, "track": trackstat, "playlist": playlist, "list": llist,
"stop": stop, "quit": quit, "launch": launch, "shuf": shuf,
"noshuf": noshuf, "ostream": ostream, "shufstat": shufstat, "-h": showHelp,
"help": showHelp
}
action.get(args[1], showHelp)()