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