Help with js code - trying to delete a track from a tracklist

To discuss development of addons / skins / customization of MediaMonkey.

Moderators: jiri, drakinite, Addon Administrators

telecore
Posts: 57
Joined: Thu Jan 28, 2021 10:20 pm

Help with js code - trying to delete a track from a tracklist

Post by telecore »

I am trying to write a .js to take a selected playlist and make a non-auto playlist copy with only the tracks that have a unique artist and title on them - to do this, I make a copy of the tracklist, then I try to delete all but one track with the same artist and title - but in the code below, when I try to delete the track from the tracklist, MM5 hangs up - I have a programming background mainly in "C" and am not familiar with some of the async and lock mechanisms involved in MM5 - can someone assist with how to get this code to work?

Code: Select all

actions.playlistScrub = {
    title: function () {
        return 'Scrub Playlist'
    },
    icon: 'remove',
    visible: function () {
        if (!window.uitools.getCanEdit())
            return false;
        else {
            var pl = resolveToValue(this.boundObject);
            return (pl.parent != undefined); // && !pl.isAutoPlaylist); // to exclude root playlists node and auto-playlists
        }
    },
    execute: function () {
        //-------------------------------------------------------
        var playlist1 = resolveToValue(this.boundObject);
        var tracklist1 = playlist1.getTracklist();

        //-------------------------------------------------------
	// create new playlist with same name + '*' same location
	// as selected playlist
        //-------------------------------------------------------
		var playlist2 = app.playlists.root.newPlaylist();
		
		playlist2.beginUpdate();
		
		playlist2.name = playlist1.name + '*';
		playlist2.isAutoPlaylist = false;
		playlist2.parent = playlist1.parent;

		playlist2.endUpdate();

		// create a new tracklist
		var tracklist2 = app.utils.createTracklist();
        
        //-------------------------------------------------------
	// copy tracklist1 to tracklist2
        //-------------------------------------------------------
        tracklist1.whenLoaded().then(function () {
           // copy the tracklist to a new tracklist
			tracklist2.addList(tracklist1);
			tracklist2.notifyLoaded();
			tracklist2.globalModifyWatch=true;
		});

        //-------------------------------------------------------
	// remove songs with same artist and title from tracklist2
        //-------------------------------------------------------
        var delCount  = 0;
        var checkCount = 0;
                
        tracklist2.whenLoaded().then(function () {
        	tracklist2.locked(function() {
        	
				var refIndex  = 1;
				var endIndex  = tracklist2.count;

				while (refIndex < endIndex)
				{
					var refTrack  = tracklist2.getValue(refIndex); //tracklist2.getFastObject(refIndex,refTrack); //
					var checkIndex = refIndex+1;
					while (checkIndex < endIndex)
					{
						var checkTrack = tracklist2.getValue(checkIndex); //tracklist2.getFastObject(checkIndex,checkTrack); //
						if (refTrack.artist===checkTrack.artist)
						{
							if (refTrack.title===checkTrack.title)
							{
								// THIS IS THE PROBLEM LINE BELOW
								//tracklist2.delete(checkIndex);
								// IT CAUSES MM5 to HANG
								delCount++;
							}
						}
						checkIndex = checkIndex + 1;
					}
					refIndex = refIndex + 1;
					checkCount++;
				}
            
            });
    
            tracklist2.commitAsync();
            uitools.toastMessage.show('check, delete count = ' + checkCount + ', ' + delCount);
        });
		
        //-------------------------------------------------------
	// copy new tracklist to new playlist
        //-------------------------------------------------------
        tracklist2.whenLoaded().then(function () {        	
        	listForEach(tracklist2, function (track2) {
            		playlist2.addTrackAsync(track2);
            });
        });
	playlist2.commitAsync();
}

};
Ludek
Posts: 4945
Joined: Fri Mar 09, 2007 9:00 am

Re: Help with js code - trying to delete a track from a tracklist

Post by Ludek »

Are you using debug build?
I guess that with debug build the hang would generate crash log like "write lock not acquired" or something like this on
tracklist2.delete(checkIndex);

The reason is that the
tracklist2.locked(function() {...
is acquiring just read lock.
For write lock use tracklist2.modifyAsync(function() {....
https://www.mediamonkey.com/webhelp/MM5 ... odifyAsync

But a better way would be to add the fullfilled tracks to the empty tracklist2 and then use
playlist.addTracksAsync( tracklist2);
on the newly created playlist.
https://www.mediamonkey.com/webhelp/MM5 ... ylist.html
Post Reply