Thread: ITunes

Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2006
    Posts
    11
    Rep Power
    0

    ITunes


    I've heard there are ways to use Python with com to control iTunes. Could someone show me how to do this in a way so I can send basic commands to iTunes (Play, Pause, Next Song, Previous Song, Volume, etc.)? I'm just interested in a basic example to work off of. Thanks.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    34
    Code:
    >>> import win32com
    >>> iTunes = win32com.client.gencache.EnsureDispatch("iTunes.Application")
    >>> iTunes.PlayFile("d:/test.mp3")
    >>> iTunes.Pause()
    
    >>> dir(iTunes)
    ['Authorize', 'BackTrack', 'CLSID', 'CanSetShuffle',
    'CanSetSongRepeat', 'CheckVersion', 'ConvertFile', 'ConvertFile2',
     'ConvertFiles', 'ConvertFiles2', 'ConvertTrack', 'ConvertTrack2',
     'ConvertTracks', 'ConvertTracks2', 'CreateEQPreset', 
    'CreateFolder', 'CreateFolderInSource', 'CreatePlaylist', 
    'CreatePlaylistInSource', 'FastForward', 'GetITObjectByID', 
    'GetPlayerButtonsState', 'GotoMusicStoreHomePage', 'NextTrack', 
    'OpenURL', 'Pause', 'Play', 'PlayFile', 'PlayPause', 
    'PlayerButtonClicked', 'PreviousTrack', 'Quit', 'Resume', 'Rewind', 
    'SetOptions', 'Stop', 'SubscribeToPodcast', 'UpdateIPod',
     'UpdatePodcastFeeds', '_ApplyTypes_', '__cmp__', '__doc__', 
    '__getattr__', '__init__', '__module__', '__repr__', '__setattr__', 
    '_get_good_object_', '_get_good_single_object_', '_oleobj_', 
    '_prop_map_get_', '_prop_map_put_', 'coclass_clsid']
    >>>
    (The win32com modules is part of PyWin32)
    Last edited by sfb; February 22nd, 2006 at 04:30 AM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2006
    Posts
    11
    Rep Power
    0
    It says there is no Win32com module. Also, how would I use the items in the brackets (Play, PlayPause, PreviousTrack, Stop, etc)
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    34
    It says there is no Win32com module.
    (The win32com modules is part of PyWin32)
    ^^^


    Also, how would I use the items in the brackets (Play, PlayPause, PreviousTrack, Stop, etc)
    Originally Posted by sfb
    Code:
    >>> dir(iTunes)
    [...]['Pause', 'Play', 'PlayFile', 'PlayPause'][...]
    
    >>> iTunes.Pause()
    ^^^
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2006
    Posts
    11
    Rep Power
    0
    Now i'm gettings this error:

    AttributeError: 'module' object has no attribute 'client'
  10. #6
  11. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Location
    Lisbon, Portugal
    Posts
    154
    Rep Power
    12
    Might be easier to help if you post the context that happens in.
    "Get it hot! Hit it harder!!!"
    --- The tutor warcry

    ActivePython
    Mark Pilgrim's Dive into Python E-book, thank this guy for his contribution to the Python community!
  12. #7
  13. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,625
    Rep Power
    4247
    Originally Posted by Hello, World.py
    Now i'm gettings this error:
    Did you type the correct case
    import win32com
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2006
    Posts
    11
    Rep Power
    0
    Originally Posted by CountVikernes
    Might be easier to help if you post the context that happens in.
    Here is the current code I tried running:

    Code:
    import win32com
    iTunes = win32com.client.gencache.EnsureDispatch("iTunes.Application")
    #iTunes.PlayFile("d:/test.mp3")
    iTunes.Pause()
    #dir(iTunes)
    #['Authorize', 'BackTrack', 'CLSID', 'CanSetShuffle','CanSetSongRepeat', 'CheckVersion', 'ConvertFile', 'ConvertFile2', 'ConvertFiles', 'ConvertFiles2', 'ConvertTrack', 'ConvertTrack2', 'ConvertTracks', 'ConvertTracks2', 'CreateEQPreset', CreateFolder', 'CreateFolderInSource', 'CreatePlaylist', 'CreatePlaylistInSource', 'FastForward', 'GetITObjectByID', GetPlayerButtonsState', 'GotoMusicStoreHomePage', 'NextTrack', 'OpenURL', 'Pause', 'Play', 'PlayFile', 'PlayPause', 'PayerButtonClicked', 'PreviousTrack', 'Quit', 'Resume', 'Rewind', 'SetOptions', 'Stop', 'SubscribeToPodcast', 'UpdateIPod',UpdatePodcastFeeds', '_ApplyTypes_', '__cmp__', '__doc__', '__getattr__', '__init__', '__module__', '__repr__', '__setattr__', '_get_good_object_', '_get_good_single_object_', '_oleobj_', '_prop_map_get_', '_prop_map_put_', 'coclass_clsid']
    Everything in the [] is on one line. I have it commented out for the time being. I thought that with that code it should pause what I have playing in iTunes. And also, the import win32com is in the right case.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    34
    Originally Posted by Hello, World.py
    Now i'm gettings this error:
    So you are. Oops

    I was working from the interpreter to get it to work, and just cut it it down to what I thought was the minimum needed and posted without trying it - Sorry.

    You need, er, something more like:

    Code:
    import win32com.client
    iTunes = win32com.client.gencache.EnsureDispatch("iTunes.Application")
    And it only worked for me when I updated to the latest iTunes - otherwise I got an error about iTunes.Application being an invalid class name...

    Everything in the [] is on one line. I have it commented out for the time being.
    That's just a Python list.

    dir(...)

    will find all the methods that you can use on an object, and return them in a list - I just did that as a demonstration of some of the other obvious things it supports (next track, etc) so I wouldn't have to find any documentation.
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2006
    Posts
    11
    Rep Power
    0
    Thanks so much. It is now working perfectly.

    P.S. Is there any documentation for the iTunes commands? The CanSetShuffle/CanSetRepeat aren't working
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    34
    Originally Posted by Hello, World.py
    Thanks so much. It is now working perfectly.

    P.S. Is there any documentation for the iTunes commands? The CanSetShuffle/CanSetRepeat aren't working
    This appears to be the official iTunes software development kit for Windows, and contains "iTunesCOM.chm" which is a big Windows Help document on the subject.

    It seems a bit raw and C++ based, but it looks usable.

    But I can't get "CanSetShuffle" to do anything useful. Hmm.
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    34
    OK, I can get it to work, but it's using COM mechanisms I don't understand.

    The quick answer is:

    Open PythonWin (it's installed with the Win32 extensions you got).
    Go to Tools -> Com MakePy Utility
    (Wait for a box to appear)
    Find and select "iTunes 1.7 Type Library"
    Click OK
    Wait for it to process.

    Now redo your script, and you'll find that you can now do:

    Code:
    ...
    iTunes.Play()
    
    playlist = iTunes.CurrentPlaylist
    
    print iTunes.CanSetShuffle(playlist)
    and it works.

    What "COM MakePy Utility" does, I don't really know. But I use it a lot.

    In PythonWin in the shell, type in these commands to control iTunes. You'll find that when you press iTunes<dot> all the methods will appear.

    and more for iTunes.Playlist.

    Very neat for playing with automating things.
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2006
    Posts
    11
    Rep Power
    0
    Well, I tried that, and all it says is "True". It doesn't toggle shuffle on/off or repeat on/off
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    34
    Try

    Code:
    iTunes.Play()
    
    playlist = iTunes.CurrentPlaylist
    
    if iTunes.CanSetShuffle(playlist):
        playlist.Shuffle = True
  28. #15
  29. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2006
    Posts
    11
    Rep Power
    0
    That worked, but replacing shuffle with repeat wouldn't.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo