[Tutor] registering itunes with events

Orri Ganel singingxduck at gmail.com
Fri Nov 17 09:14:25 CET 2006


Hello all,

As I mentioned in my recent emails, I've been working on a program that
will display the currently playing iTunes track (for example, when
iTunes is minimized to the system tray so that you don't have to enlarge
it again just to see the name of the song).  Unfortunately, of the
hundreds of configurations I've found online, not one seems able to
properly register iTunes with events, which would allow me to do away
with the infinite while loop and time.sleep(1) and instead just call the
update method whenever OnPlayerPlayEvent or OnPlayerStopEvent got
called.  I was playing around with it and managed for a bit to get it
working, but all subsequent attempts to replicate the phenomenon have
been fruitless.  My best guess is something along the lines of:

iTunes = win32com.client.gencache.EnsureDispatch("iTunes.Application")
iTunesEvents = win32com.client.WithEvents(iTunes, iTunesEventHandler)

where iTunesEventHandler is a class with methods OnPlayerPlayEvent,
etc.  From what I can glean, it seems like I'm missing some essential
step that will keep the events connection open b/c the above tends to
just close after calling iTunesEventHandler.__init__() Also, pythoncom
seems to play a role in some of the online scripts I've found, but none
of them seem to do anything.  The trend seems to be:

pythoncom.CoInitialize()
iTunes = win32com.client.gencache.EnsureDispatch("iTunes.Application")
iTunesEvents = win32com.client.WithEvents(iTunes, iTunesEventHandler)

Sometimes followed with

pythoncom.PumpWaitingMessages()

Right now, I'd just like to be able to keep a connection open with
iTunes that will catch OnPlayerPlayEvents and call the correct method.
So if anyone has any idea how to do that, I'd be grateful.

Thanks,
Orri



More information about the Tutor mailing list