1 | #!/usr/bin/python
|
---|
2 | # -*- coding: utf-8 -*-
|
---|
3 |
|
---|
4 | from appscript import *
|
---|
5 | from aem import AEEnum
|
---|
6 | import sys
|
---|
7 | import time
|
---|
8 |
|
---|
9 | def iTunes_main_pane():
|
---|
10 | return app(u'System Events').application_processes[u'iTunes'].windows[u'iTunes'].scroll_areas[1]
|
---|
11 |
|
---|
12 | def iOS_devices(iTunes):
|
---|
13 | return iTunes.sources[its.kind == AEEnum('kPod')]
|
---|
14 |
|
---|
15 | def wait_for_podcast_update(iTunes):
|
---|
16 | # show 'Podcasts'
|
---|
17 | iTunes.playlists[its.special_kind == AEEnum('kSpP')].reveal()
|
---|
18 | podcast_status = iTunes_main_pane().outlines[1].rows.static_texts[1].value
|
---|
19 | # XXX this can fail if iTunes is hidden; fix it
|
---|
20 | while any(status in (u'downloading', u'queued for download')
|
---|
21 | for status in podcast_status.get()):
|
---|
22 | time.sleep(0.5)
|
---|
23 |
|
---|
24 | def sync_devices(iTunes):
|
---|
25 | try:
|
---|
26 | iOS_devices(iTunes).update()
|
---|
27 | except CommandError:
|
---|
28 | print >> sys.stderr, 'No iOS devices detected. Waiting for an iOS device...'
|
---|
29 | while True:
|
---|
30 | time.sleep(0.5)
|
---|
31 | try:
|
---|
32 | iOS_devices(iTunes).update()
|
---|
33 | return
|
---|
34 | except CommandError:
|
---|
35 | pass
|
---|
36 |
|
---|
37 | # show first iOS device
|
---|
38 | iOS_devices(iTunes).sources[1].library_playlists[1].reveal()
|
---|
39 |
|
---|
40 | # wait for update to complete - XXX wait for all
|
---|
41 | def sync_enabled():
|
---|
42 | try:
|
---|
43 | iTunes_main_pane().buttons[u'Apply'].click()
|
---|
44 | except CommandError:
|
---|
45 | pass
|
---|
46 | try:
|
---|
47 | return iTunes_main_pane().buttons[u'Sync'].enabled.get()
|
---|
48 | except CommandError:
|
---|
49 | pass
|
---|
50 |
|
---|
51 | while not sync_enabled():
|
---|
52 | time.sleep(0.5)
|
---|
53 |
|
---|
54 | if __name__ == '__main__':
|
---|
55 | iTunes = app('iTunes')
|
---|
56 | print >> sys.stderr, 'Starting podcast update...'
|
---|
57 | iTunes.updateAllPodcasts()
|
---|
58 | print >> sys.stderr, 'Synchronizing iOS devices...'
|
---|
59 | sync_devices(iTunes)
|
---|
60 | print >> sys.stderr, 'Updating podcasts...'
|
---|
61 | iTunes.updateAllPodcasts()
|
---|
62 | wait_for_podcast_update(iTunes)
|
---|
63 | print >> sys.stderr, 'Synchronizing iOS devices...'
|
---|
64 | sync_devices(iTunes)
|
---|
65 | app(id='net.sabi.UpdatePodcasts').quit()
|
---|