-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiTunes.py
93 lines (76 loc) · 2.27 KB
/
iTunes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# -*- coding: utf-8 -*-
from Foundation import *
from ScriptingBridge import *
class iTunes(object):
"""
A helper class for interacting with iTunes on Mac OS X via Scripting
Bridge framework.
To use this, launch iTunes and make sure a playlist or an album is ready.
Usage:
>>> player = iTunes()
>>> player.status
'playing'
>>> player.current_track
u'Maison Rilax'
>>> player.current_album
u'Maison Rilax'
>>> player.current_artist
u'Lemonator'
>>> player.pause()
>>> player.status
'paused'
>>> player.play()
>>> player.next()
>>> player.current_track
u'Not Your Game'
"""
def __init__(self):
self.app = SBApplication.applicationWithBundleIdentifier_("com.apple.iTunes")
self.playlists = self.app.sources()[0].playlists()
self.playlist_names = [item.name() for item in self.playlists]
def play_playlist(self,name):
try:
index = self.playlist_names.index(name)
except:
return False
#maybe?
try:
self.currentPlaylist.shuffle = True
except:
pass
self.playlists[index].playOnce_(False)
return True
@property
def status(self):
if self.app.playerState() == 1800426320:
return "playing"
elif self.app.playerState() == 1800426352:
return "paused"
else:
return "unknown"
@property
def current_track(self):
return self.app.currentTrack().name()
@property
def current_artist(self):
return self.app.currentTrack().artist()
@property
def current_album(self):
return self.app.currentTrack().album()
@property
def volume(self):
return self.app.soundVolume()
@volume.setter
def volume(self, level):
self.app.setSoundVolume_(level)
def pause(self):
self.app.pause()
def play(self):
# According to AppleScript documentatin there should be a .play()
# method, but apparently there isn't. So we fake it :)
if self.status == "paused":
self.app.playpause()
def next(self):
self.app.nextTrack()
def previous(self):
self.app.previousTrack()