Anyone got a sample script for playlist edit?

This forum is for questions / discussions regarding development of addons / tweaks for MediaMonkey for Windows 4.

Moderators: Gurus, Addon Administrators

chrisjj
Posts: 5007
Joined: Wed Feb 14, 2007 5:14 pm
Location: UK

Anyone got a sample script for playlist edit?

Post by chrisjj »

I want to write a simple script that replaces the selected track in a playlist with another track from the library. Does anyone have/know a sample script that does any kind of playlist reference read/write, I from which I can start? I found playlist scripting rather tricky last time I tried, so a sample would be most appreciated.

Thanks.
Last edited by Lowlander on Mon Sep 29, 2014 10:08 am, edited 1 time in total.
Reason: Moved to correct forum
Chris
Eyal
Posts: 3116
Joined: Sun Jun 26, 2005 9:27 am
Location: Québec

Re: Anyone got a sample script for playlist edit?

Post by Eyal »

Do you really need a script to do such an easy thing? - IMO it would be it would be far more extensive to use a script rather than using Copy/Paste.
Skins for MediaMonkey: Cafe, Carbon, Helium, Spotify, Zekton. [ Wiki Zone ].
chrisjj
Posts: 5007
Joined: Wed Feb 14, 2007 5:14 pm
Location: UK

Re: Anyone got a sample script for playlist edit?

Post by chrisjj »

Eyal wrote:Do you really need a script to do such an easy thing?
No. I need a script to do such a hard thing.
Eyal wrote: IMO it would be it would be far more extensive to use a script rather than using Copy/Paste.
No. There are hundreds of replacements to be made.

Thanks anyway for the suggestion.
Chris
Eyal
Posts: 3116
Joined: Sun Jun 26, 2005 9:27 am
Location: Québec

Re: Anyone got a sample script for playlist edit?

Post by Eyal »

OK I understand. You want to replace one track appearing in multiple playlists. That would legitimate a script.
Skins for MediaMonkey: Cafe, Carbon, Helium, Spotify, Zekton. [ Wiki Zone ].
chrisjj
Posts: 5007
Joined: Wed Feb 14, 2007 5:14 pm
Location: UK

Re: Anyone got a sample script for playlist edit?

Post by chrisjj »

Eyal wrote:OK I understand. You want to replace one track appearing in multiple playlists.
And I'll settle for replacing one track's dozens' of occurrences in one playlist e.g. here: http://www.chrisjj.com/tango/cjjsets/20 ... illon.html
Chris
mcow
Posts: 834
Joined: Sun Sep 21, 2008 9:35 pm
Location: Cupertino, California

Re: Anyone got a sample script for playlist edit?

Post by mcow »

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.
Post Reply