Add a song to iTunes (1 Viewer)

speakers_86

Registered User.
Local time
Today, 01:19
Joined
May 17, 2007
Messages
1,919
I have code that works, but when I try to simplify it, it doesn't. I mostly made this myself, but I did have some kind of reference I was looking at. Could somone tell me why?



Code:
Private Sub CreatePlaylist()
    Set objapp = CreateObject("iTunes.Application")
    Set colSources = objapp.sources
    Set objSource = colSources.ItemByName("Library")
    Set colPlaylists = objSource.Playlists
    Set objplaylist = colPlaylists.ItemByName("Muse Playlist")
    Set objLibrary = objapp.LibraryPlaylist
    Set coltracks = objLibrary.Tracks
  
    'Create Playlist
    Set objplaylist = objapp.CreatePlaylist("Muse Playlist")

    'Add first track and start playing
    Set objsong = coltracks.ItemByName("Some Song Name")
    objplaylist.AddTrack (objsong)
    'Play playlist

    iTunes.LibrarySource.Playlists.ItemByName("Muse Playlist").PlayFirstTrack
End Sub

The first question is what should those variables be declared as? Objects?

My next question is that why does that work, but this doesn't?
Code:
CreateObject("itunes.application").sources.ItemByName("Library").Playlists.ItemByName("Muse Playlist").AddTrack (objsong)

I have tried that, and even using some other variations with variables, but it never works. Why does the code above work but this does not?

The third question is about setting variables to Nothing. What things do we need to set to nothing? I know recordsets get set to nothing when done, but what of other types of variables?
 

DavidAtWork

Registered User.
Local time
Today, 06:19
Joined
Oct 25, 2011
Messages
699
1. All the variables you've listed should be declared as Objects.
2. I think the reason the one line approach doesn't work is because you don't have an object to set the application to. I don't think you can just run an application, I think it needs to be bound to an object.
3. All the object variables should be set to nothing, it just releases the memory which is assigned when you declare a variable.
David
 

speakers_86

Registered User.
Local time
Today, 01:19
Joined
May 17, 2007
Messages
1,919
So this works, though I thought I tried this:
Code:
objapp.sources.ItemByName("Library").Playlists.ItemByName("Muse Playlist").AddTrack (objsong)

What is wrong here? I'm getting an object variable or with block variable not set, with both methods.
Code:
'    Dim itunes As Object
'    Set itunes = CreateObject("iTunes.Application")
    Dim itunes As iTunesApp
    Set itunes = New iTunesApp
    
    itunes.sources.ItemByName("Library").Playlists.ItemByName("Muse Playlist").AddTrack ("Big Casino")


edit- I'll try again after work in a blank db. There could be something else interfering with what I'm trying to do.
 
Last edited:

speakers_86

Registered User.
Local time
Today, 01:19
Joined
May 17, 2007
Messages
1,919
Okay, so I managed to simplify it.

Code:
    Set itunes = CreateObject("itunes.application")
    Set playlist = itunes.CreatePlaylist("Muse Playlist")
    Set songname = itunes.libraryplaylist.tracks.itembyname("Big Casino")
    playlist.addtrack (songname)

What I can't seem to figure out is how to select an existing playlist and add a song to that. If you try and select an existing playlist, there is no add track method. Perhaps existing stuff is read only. That doesn't seem right either, because intellisense does not show an add track method after create playlist. I'm not sure why it works in this case considering that.
 

speakers_86

Registered User.
Local time
Today, 01:19
Joined
May 17, 2007
Messages
1,919
I tried to look at the sdk, but it looks like you have to pay an exorbant amount of money to see it. It was free the first time I played with this a few years ago, but it was new also. I think it has changed a lot.

edit- Maybe IITPlaylist is what I needed. I know i played with IITPlaylistCollection, but as that article says, IITPlaylist is not exposed, so I didn't know it was there.
 

vbaInet

AWF VIP
Local time
Today, 06:19
Joined
Jan 22, 2010
Messages
26,374
And what about the sample code from the site I gave?
 

speakers_86

Registered User.
Local time
Today, 01:19
Joined
May 17, 2007
Messages
1,919
It's a good reference, but he doesn't use the AddTrack method at all. It also shows that most people that code with iTunes don't understand everything. I especially like this comment:
' Copy the playlist over to a userplaylist, dunno how or why this works
154 ' but you get access to more information, like specialkind and parent

edit- I didn't try it, but according to this ITPlaylist does not have an AddTrack method.

edit- Here's a good resource too. Using the iTunesEvents as listed at the bottom of that link, can an event in iTunes trigger an action in Access?
 
Last edited:

Users who are viewing this thread

Top Bottom