source: trunk/Update Podcasts/update_podcasts.py@ 670

Last change on this file since 670 was 640, checked in by Nicholas Riley, 14 years ago

Actually update all the iOS devices.

The code makes an assumption that the first iOS device finishes
syncing last. This is actually the case for me as my iPod touch is
much slower at syncing, so I may not fix it for a while.

File size: 2.0 KB
Line 
1#!/usr/bin/python
2# -*- coding: utf-8 -*-
3
4from appscript import *
5from aem import AEEnum
6import sys
7import time
8
9def iTunes_main_pane():
10 return app(u'System Events').application_processes[u'iTunes'].windows[u'iTunes'].scroll_areas[1]
11
12def iOS_devices(iTunes):
13 return iTunes.sources[its.kind == AEEnum('kPod')]
14
15def 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
24def 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
54if __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()
Note: See TracBrowser for help on using the repository browser.