#!/usr/bin/python # -*- coding: utf-8 -*- from appscript import * from aem import AEEnum import sys import time def iTunes_main_pane(): return app(u'System Events').application_processes[u'iTunes'].windows[u'iTunes'].scroll_areas[1] def wait_for_podcast_update(iTunes): # show 'Podcasts' iTunes.playlists[its.special_kind == AEEnum('kSpP')].reveal() podcast_status = iTunes_main_pane().outlines[1].rows.static_texts[1].value # XXX this can fail if iTunes is hidden; fix it while any(status in (u'downloading', u'queued for download') for status in podcast_status.get()): time.sleep(0.5) def sync_devices(iTunes): try: iTunes.update() except CommandError: print >> sys.stderr, 'No iOS devices detected. Waiting for an iOS device...' while True: time.sleep(0.5) try: iTunes.update() return except CommandError: pass # show first iOS device iTunes.sources[its.kind == AEEnum('kPod')].sources[1].library_playlists[1].reveal() # wait for update to complete def sync_enabled(): try: iTunes_main_pane().buttons[u'Apply'].click() except CommandError: pass try: return iTunes_main_pane().buttons[u'Sync'].enabled.get() except CommandError: pass while not sync_enabled(): time.sleep(0.5) if __name__ == '__main__': iTunes = app('iTunes') print >> sys.stderr, 'Starting podcast update...' iTunes.updateAllPodcasts() print >> sys.stderr, 'Synchronizing iOS devices...' sync_devices(iTunes) print >> sys.stderr, 'Updating podcasts...' iTunes.updateAllPodcasts() wait_for_podcast_update(iTunes) print >> sys.stderr, 'Synchronizing iOS devices...' sync_devices(iTunes) app(id='net.sabi.UpdatePodcasts').quit()