Author: holler
(2008/11/17 02:19) Over 3 years ago
0.2.1
1
#!/usr/bin/python
2
###################################################
3
#
4
# iTunes Python Shell Script v0.2
# iTunes Python Shell Script v0.2.1
5
# http://blog.jadecell.org/
6
7
# inspired by the bash shell scripts on MacOSXHints
...
10
# README
11
# - give read/execute permissions to the file (ex 'chmod 755 ./itunes')
12
# - import this script into your bin directory (ex '/usr/bin' or '~/bin')
13
# - alternatively you can make a symbolic link:
14
# sudo ln -s /path/to/your/mypytunes/itunes.py /usr/bin/itunes
15
from os import system as cmd
16
from sys import argv as args
17
18
# Main Script function to run
19
def pyTunes(args):
def pyTunes():
20
"""Main Script function to run"""
21
try: args[1] # if no argument
22
except: defHelp() # show help definition and exit
23
24
if(mDefs.has_key(args[1])): # Verify if argument is available and execute
25
mounter()
26
mounter(args)
else: # otherwise show list of arguments available
27
print "\n** invalid argument **\n"
28
showHelp()
29
# Raise exception and exit out of program
def myException(x):
30
31
"""Raise exception and exit out of the program"""
32
print x
33
raise SystemExit
34
# Minimal Help function (tells user to run 'help' for list of arguments)
def defHelp():
35
myException(" No arguments provided - Usage %s <option>\n for a list of commands: %s -h"%(args[0],args[0]))
36
"""Minimal Help function (tells user to run 'help' for list of arguments)"""
# Help function
37
38
myException(" No arguments provided - Usage %s <option>\n For a list of commands: %s help"%(args[0],args[0]))
def showHelp():
39
40
"""Help function showing a list of commands when '<script> -h' is run"""
41
helpHeader = "---------------------------------------------------"
42
print helpHeader+"\n iTunes Command Line Interface\n"+helpHeader
43
print "Usage: %s <option>\n"%args[0]
44
49
x += mDefs[i]['h']
55
50
56
51
myException(helpHeader)
57
52
# Main function for iTunes execution
58
def mounter(args):
53
def mounter():
59
"""iTunes execution"""
54
v = mDefs[args[1]]
60
try:
61
cmd("osascript -e 'tell application \"iTunes\" "+v['c']+"'")
62
v['r']()
66
except KeyError:
67
print "invalid 2"
68
63
# Play streaming location
64
def ostream():
69
70
"""Play streaming location"""
65
71
print "Playing playlist %s"%(args[2])
72
#cmd('osascript -e \'tell application "iTunes"\' -e "open location \"%s\"" -e "end tell"\''%(args[2]))
73
cmd('osascript -e \'tell application "iTunes"\' -e \'open location "%s"\' -e \'end tell\''%(args[2]))
74
except:
75
myException("Please provide the stream location you want to play")
76
# Show iTunes player status (play/pause)
def status():
77
78
cmd('osascript -e \'tell application "iTunes" to player state as string\' > /tmp/iTuneState')
79
f = open('/tmp/iTuneState', 'r')
80
s = f.readline().strip()
81
shufstat()
84
if s == "playing":
85
trackstat()
86
# Show Playlist, Artist, and Track that is currently playing
82
def trackstat():
87
88
"""Show Playlist, Artist, and Track that is currently playing"""
83
cmd('osascript -e \'tell application "iTunes" to name of current playlist as string\' > /tmp/iTunesPlaylist')
89
cmd('osascript -e \'tell application "iTunes" to artist of current track as string\' >> /tmp/iTunesPlaylist')
90
cmd('osascript -e \'tell application "iTunes" to name of current track as string\' >> /tmp/iTunesPlaylist')
91
x,y,z,a = s.split('\n')
94
print "Current track %s: %s, on playlist \"%s\""%(y,z,x)
95
cmd('rm /tmp/iTunesPlaylist')
96
# Show the shuffle status (on/off)
92
def shufstat():
97
98
"""Show the shuffle status (on/off)"""
93
cmd("osascript -e 'tell application \"iTunes\" to get shuffle of current playlist' > /tmp/iTunesShuffle")
99
f = open('/tmp/iTunesShuffle', 'r')
100
101
print "Shuffling: %s"%(s)
102
cmd('rm /tmp/iTunesShuffle')
103
# Change volume
def vol():
104
105
"""Change Volume"""
print "Changing iTunes volume level."
106
cmd('osascript -e \'tell application "iTunes" to get (sound volume as integer)\' > /tmp/iTunesVol')
107
f = open('/tmp/iTunesVol')
108
115
cmd('osascript -e \'tell application "iTunes" to set sound volume to (%i as integer)\''%(newvol))
121
116
print "from %i to %i"%(s,newvol)
122
117
cmd('rm /tmp/iTunesVol')
123
118
# Change playlist
119
def playlist():
124
125
"""Change playlist"""
120
126
print "Changing iTunes plalist to %s"%(args[2])
127
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]))
128
129
myException("Pleasae define the playlist you want to play")
130
# List playlists available on iTunes
def llist():
131
132
"""List playlists available on iTunes"""
print "Listing available playlists:"
133
cmd('osascript -e \'tell application "iTunes"\' -e \'name of every user playlist of source "Library"\' -e "end tell" > /tmp/iTunesPlaylists')
134
f = open('/tmp/iTunesPlaylists')
135
242
}
248
243
249
244
if __name__ == "__main__":
250
245
pyTunes()
251
pyTunes(args)
# iTunes Python Shell Script v0.2.1# Main Script function to rundef pyTunes(args):def pyTunes():"""Main Script function to run"""mounter(args)# Raise exception and exit out of program# Minimal Help function (tells user to run 'help' for list of arguments)myException(" No arguments provided - Usage %s <option>\n for a list of commands: %s -h"%(args[0],args[0]))"""Minimal Help function (tells user to run 'help' for list of arguments)"""# Help functionmyException(" No arguments provided - Usage %s <option>\n For a list of commands: %s help"%(args[0],args[0]))# Main function for iTunes executiondef mounter(args):def mounter():"""iTunes execution"""cmd("osascript -e 'tell application \"iTunes\" "+v['c']+"'")cmd("osascript -e 'tell application \"iTunes\" "+v['c']+"'")# Play streaming location#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]))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]))myException("Please provide the stream location you want to play")myException("Please provide the stream location you want to play")# Show iTunes player status (play/pause)cmd('osascript -e \'tell application "iTunes" to player state as string\' > /tmp/iTuneState')cmd('osascript -e \'tell application "iTunes" to player state as string\' > /tmp/iTuneState')f = open('/tmp/iTuneState', 'r')f = open('/tmp/iTuneState', 'r')# Show Playlist, Artist, and Track that is currently playingcmd('osascript -e \'tell application "iTunes" to name of current playlist as string\' > /tmp/iTunesPlaylist')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 artist of current track as string\' >> /tmp/iTunesPlaylist')cmd('osascript -e \'tell application "iTunes" to name of current track as string\' >> /tmp/iTunesPlaylist')cmd('osascript -e \'tell application "iTunes" to name of current track as string\' >> /tmp/iTunesPlaylist')x,y,z,a = s.split('\n')x,y,z,a = s.split('\n')cmd('rm /tmp/iTunesPlaylist')cmd('rm /tmp/iTunesPlaylist')# Show the shuffle status (on/off)cmd("osascript -e 'tell application \"iTunes\" to get shuffle of current playlist' > /tmp/iTunesShuffle")cmd("osascript -e 'tell application \"iTunes\" to get shuffle of current playlist' > /tmp/iTunesShuffle")f = open('/tmp/iTunesShuffle', 'r')f = open('/tmp/iTunesShuffle', 'r')cmd('rm /tmp/iTunesShuffle')cmd('rm /tmp/iTunesShuffle')# Change volumecmd('osascript -e \'tell application "iTunes" to get (sound volume as integer)\' > /tmp/iTunesVol')cmd('osascript -e \'tell application "iTunes" to get (sound volume as integer)\' > /tmp/iTunesVol')f = open('/tmp/iTunesVol')f = open('/tmp/iTunesVol')cmd('osascript -e \'tell application "iTunes" to set sound volume to (%i as integer)\''%(newvol))cmd('osascript -e \'tell application "iTunes" to set sound volume to (%i as integer)\''%(newvol))cmd('rm /tmp/iTunesVol')cmd('rm /tmp/iTunesVol')# Change playlistcmd('osascript -e \'tell application "iTunes"\' -e \'set the new_playlist to "%s" as string\' -e "play playlist new_playlist" -e "end tell"'%(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]))myException("Pleasae define the playlist you want to play")myException("Pleasae define the playlist you want to play")# List playlists available on iTunescmd('osascript -e \'tell application "iTunes"\' -e \'name of every user playlist of source "Library"\' -e "end tell" > /tmp/iTunesPlaylists')cmd('osascript -e \'tell application "iTunes"\' -e \'name of every user playlist of source "Library"\' -e "end tell" > /tmp/iTunesPlaylists')f = open('/tmp/iTunesPlaylists')f = open('/tmp/iTunesPlaylists')pyTunes(args)