by mcow » Mon Nov 03, 2014 2:15 pm
I just wrote a little Python script that opens a specific playlist, steps thru the tracks looking for a particular title, and replaces matching tracks with (one particular) different track. I created a playlist with the name "chrisjj", with a handful of random tracks including two instances of the "A Simple Answer" track; when I run it, the two instances have been replaced with the "Girl" track, and no errors.
Code: Select all
import win32com.client
SDB = win32com.client.Dispatch('SongsDB.SDBApplication')
SDB.ShutdownAfterDisconnect = False
def Replace():
# Get playlist to process
pl = SDB.PlaylistByTitle("chrisjj")
if pl.Tracks.Count == 0: # empty (includes nonexistent)
return
# Get track to use as replacement
query = "SongTitle = 'Girl' AND Artist = 'Beck'"
newsong = SDB.Database.QuerySongs(query)
if not newsong.Item: # verify at least one track matches query
return
# depending on the query used, may need to ensure that you have the right track
# newsong is an iterator, which can be advanced with newsong.Next()
# name of title of tracks to be replaced
rtitle = "A Simple Answer" # by Grizzly Bear
index = 0;
for song in pl.Tracks:
if song is None:
break
# depending on contents of list, this test may need to be more complicated
if song.Title == rtitle: # Is this the song we want to replace?
pl.RemoveTrack(song)
pl.InsertTrack(index, newsong.Item)
index += 1
# when invoked from command line, execute the function
if __name__ == '__main__':
Replace()
How you identify the track(s) to replace and the track(s) to replace them with will, of course, be different, but the core logic of the loop works.
This technique is a little hazardous, I think, because it's changing the playlist's list of tracks at the same time it's stepping across that list.
It would probably be safer to either copy the list of tracks to scan across, while modifying the actual playlist, or, build a new list of tracks as I scan (copying or substituting each track as desired) and then remove all tracks from the playlist and replace with the new list.
But, this does work. It is not fast, but I bet the build-a-new-list technique would be faster if that's a problem.
I just wrote a little Python script that opens a specific playlist, steps thru the tracks looking for a particular title, and replaces matching tracks with (one particular) different track. I created a playlist with the name "chrisjj", with a handful of random tracks including two instances of the "A Simple Answer" track; when I run it, the two instances have been replaced with the "Girl" track, and no errors.
[code]import win32com.client
SDB = win32com.client.Dispatch('SongsDB.SDBApplication')
SDB.ShutdownAfterDisconnect = False
def Replace():
# Get playlist to process
pl = SDB.PlaylistByTitle("chrisjj")
if pl.Tracks.Count == 0: # empty (includes nonexistent)
return
# Get track to use as replacement
query = "SongTitle = 'Girl' AND Artist = 'Beck'"
newsong = SDB.Database.QuerySongs(query)
if not newsong.Item: # verify at least one track matches query
return
# depending on the query used, may need to ensure that you have the right track
# newsong is an iterator, which can be advanced with newsong.Next()
# name of title of tracks to be replaced
rtitle = "A Simple Answer" # by Grizzly Bear
index = 0;
for song in pl.Tracks:
if song is None:
break
# depending on contents of list, this test may need to be more complicated
if song.Title == rtitle: # Is this the song we want to replace?
pl.RemoveTrack(song)
pl.InsertTrack(index, newsong.Item)
index += 1
# when invoked from command line, execute the function
if __name__ == '__main__':
Replace()
[/code]
How you identify the track(s) to replace and the track(s) to replace them with will, of course, be different, but the core logic of the loop works.
This technique is a little hazardous, I think, because it's changing the playlist's list of tracks at the same time it's stepping across that list.
It would probably be safer to either copy the list of tracks to scan across, while modifying the actual playlist, or, build a new list of tracks as I scan (copying or substituting each track as desired) and then remove all tracks from the playlist and replace with the new list.
But, this does work. It is not fast, but I bet the build-a-new-list technique would be faster if that's a problem.