How can i change an Object type ?

K

KuhlmannSascha

Hi,

i tried now for several hours to read through a win32com API to access
Itunes and read out myplaylists.

First of all the Code:

import os, sys, pythoncom, win32com.client, pywintypes, sets

def showplaylists():
iTunes = win32com.client.Dispatch("iTunes.Application")
playlists = iTunes.LibrarySource.Playlists
numPlaylists = playlists.Count
while (numPlaylists != 0):
curPlaylist = playlists.Item(numPlaylists)
if curPlaylist.Kind == 2:
if curPlaylist.Smart():
numPlaylists -= 1
else:
print curPlaylist.Name
numtrack = curPlaylist.Tracks.Count
while (numtrack !=0):
curPlaylist.Tracks(numtrack).Name
numtrack -= 1
numPlaylists -= 1
showplaylists()

The current Logic is to access first Itunes and then a Playlist
Collection.
This Playlist collection returns different kind of objects for
Playlists.
I am focussing on the Playlists of the object UserPlaylist.
(CodeLine: if curPlaylist.Kind == 2:)
After that i should be sure to have a UserPlaylist, but Python stops
with an exception that the requested Attribute "Smart" is not
available
Error MEssage:
AttributeError: '<win32com.gen_py.iTunes 1.9 Type Library.IITPlaylist
instance at 0x30216960>' object has no attribute 'Smart'


Any help is appreciated......

Regards
Sascha
 
J

Jerry Hill

I am focussing on the Playlists of the object UserPlaylist.
(CodeLine: if curPlaylist.Kind == 2:)
After that i should be sure to have a UserPlaylist, but Python stops
with an exception that the requested Attribute "Smart" is not
available

It looks like win32com thinks you have a IITPlaylist, and you really
have a subclass of that, a IITUserPlaylist. You need to cast it like
this:

import win32com.client
iTunes = win32com.client.gencache.EnsureDispatch('iTunes.Application')
playlists = iTunes.LibrarySource.Playlists
for playlist in playlists:
if playlist.Kind == 2:
playlist = win32com.client.CastTo(playlist, 'IITUserPlaylist')
print "Name: ", playlist.Name
print "Kind: ", playlist.Kind
print "Smart: ", playlist.Smart
print
 
T

Tim Roberts

KuhlmannSascha said:
i tried now for several hours to read through a win32com API to access
Itunes and read out myplaylists.

First of all the Code:
...
The current Logic is to access first Itunes and then a Playlist
Collection.
This Playlist collection returns different kind of objects for
Playlists.
I am focussing on the Playlists of the object UserPlaylist.
(CodeLine: if curPlaylist.Kind == 2:)
After that i should be sure to have a UserPlaylist, but Python stops
with an exception that the requested Attribute "Smart" is not
available
Error MEssage:
AttributeError: '<win32com.gen_py.iTunes 1.9 Type Library.IITPlaylist
instance at 0x30216960>' object has no attribute 'Smart'

Smart is part of IITUserPlaylist, not IITPlaylist. You need to call
curPlaylist.QueryInterface to get the IITUserPlaylist, but that means
you'll need to know the GUID for IITUserPlaylist. Perhaps Google will
help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top