Page 1 of 2

Randomise Playlist

Posted: Thu May 13, 2021 3:56 am
by mmain
With MMW4 I had a script which allowed me to randomise a playlist. Is there anyway I can recreate a similar script under MMW5?

Re: Randomise Playlist

Posted: Thu May 13, 2021 12:23 pm
by drakinite
When you say randomize playlist, do you mean playing it in shuffle mode? Or to actually rearrange the order of tracks on the playlist?

Re: Randomise Playlist

Posted: Fri May 14, 2021 3:59 am
by mmain
Not shuffle, randomise order of playlist. I used to have a script in MMW4 which did this, but scripting seems to have gone from MMW5

Re: Randomise Playlist

Posted: Fri May 14, 2021 10:25 am
by Lowlander
Scripting is still possible with MediaMonkey. However MediaMonkey 4 scripts are incompatible as MediaMonkey was rewritten to be cross-platform compatible instead of just a Windows application. It will take time till more Addons are created, it did so for MediaMonkey 3/4 too.

Re: Randomise Playlist

Posted: Sat May 15, 2021 3:47 am
by Andre_H
There's a built-in functionality for shuffle, at least at the "smart playlists": in the filter options, you can define sort-options, and there are some "random song (automatic refresh)" options. (i run the german version, so i can only guess what it's named in english, just have a look).

the refresh/shuffle definitly works on "click on playlist" in MediaMonkey, and from what i see when syncing to my mobile device, there seems to bee some kind of automatic/timed refresh/shuffle, but i'm not sure how that works in detail. maybe one of the devs could explain?

Re: Randomise Playlist

Posted: Mon May 17, 2021 4:48 am
by Ludek
As for using 'Playlist is' condition in an auto-playlist and using [x] Limit ... selected by [Random (refresh-all)]
Yes, this can be used as workaround without a need for scripting.

But I guess that user "mmmain" is asking for a script that randomizes static playlist directly (without a need using auto-playlists) ?

Then I think that code like this should work:

Code: Select all

var tracks = playlist.getTracklist();
tracks.whenLoaded().then(()=> {
  tracks.randomize();
  playlist.reorderAsync(tracks);
});
The true is that neither the randomize and reorderAsync are documented atm. I'll update the docs.

Re: Randomise Playlist

Posted: Mon May 31, 2021 10:53 pm
by mmain
Is there some guidance on how to add this as a script?

Re: Randomise Playlist

Posted: Fri Jun 04, 2021 5:10 pm
by drakinite
Yep! You can find a step by step guide on how to create an addon/script here: https://www.mediamonkey.com/wiki/Gettin ... d_(Addons)

One thing you'd have to decide is where exactly you want the "randomise playlist" action to be accessible.
If you want it to be accessible in that little context menu, here are the steps I'd take to figure out how to make the script into an addon:

1) To find out exactly where in the code is responsible for that context menu, I can do a text search (in the installation directory, through all the .js files) for one of the items: in this case, "Remove duplicates".
2) I then find that the text "Remove duplicates" is defined in actions.playlistRemoveDuplicates. Good! Now do a text search for playlistRemoveDuplicates, to see where that action is called.
3) Bingo! There's one inside playlistHeader.js, where it creates this.btnMenu.controlClass.menuArray. That's what we're looking for!

So what we need to do now is:
- Create a new "action", similar to actions.playlistRemoveDuplicates, which randomizes the playlist
- Add that "action" to the menu defined in playlistHeader.js.

4) So to create a new action, let's create a new file in our project folder (where you placed info.json; take a look at the Hello World example in the getting started guide) named actions_add.js. We then define, let's call it , "playlistRandomise".

To start with, let's define actions.playlistRandomise:

Code: Select all

actions.playlistRandomise = {  }
In fact, actions.playlistRemoveDuplicates is a very good baseline, and we can keep most of its properties. We just need to change its title, and preferably its icon. Let's go with shuffle. Also, the contents of its execute function is pretty close to what we need already. It gets the associated playlist object, and even loads its tracklist. So let's put Ludek's code inside the whenLoaded callback, careful to rename the variables so that we don't encounter an error:

Code: Select all

actions.playlistRandomise = {
    title: function () {
        return 'Randomise Playlist'
    },
    icon: 'shuffle',
    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 playlist = resolveToValue(this.boundObject);
        var tracks = playlist.getTracklist();
        tracks.whenLoaded().then(() => {
            tracks.randomize();
            playlist.reorderAsync(tracks);
        });
    }
};
5) Now, we need to add it to the playlist header menu that we talked about before. To do this, we'll use the Override method as discussed here: https://www.mediamonkey.com/wiki/Import ... )#Override So here, we will create playlistHeader_add.js (within a "controls" subfolder).

To start with, this is the format you need to do to override the PlaylistHeader's _initButtons method, inside controls/playlistHeader_add.js:

Code: Select all

PlaylistHeader.prototype.override({
    _initButtons: function ($super) {
        $super();
        // now we do our own stuff
    }
});
If we want our item to be ordered directly after "Remove duplicates", we'll want to order it correctly. "Remove Duplicates" has a grouporder of 10 and an order of 20, and "Pin it" has a grouporder of 10 and an order of 30. So we'll want our order to be between those two; so we'll do 25.

Now, as we were using playlistRemoveDuplicates as a baseline, you can see that it uses a function called bindAction. This is so that the actions can figure out which playlist it's supposed to be editing (and that's where the "resolveToValue(this.boundObject)" comes in to play). So let's do the same for our new menu item:

Code: Select all

PlaylistHeader.prototype.override({
    _initButtons: function ($super) {
        $super();
        var _this = this;
        this.btnMenu.controlClass.menuArray.push({
            action: bindAction(window.actions.playlistRandomise, () => {
                return _this._playlist;
            }),
            order: 25,
            grouporder: 10
        });
    }
});
6) Now, pack your MMIP, install it, and test it! https://lambda.sx/IdwS.mp4

Re: Randomise Playlist

Posted: Fri Jun 11, 2021 2:31 am
by mmain
Thanks for your support and guidance, works a treat :D

Re: Randomise Playlist

Posted: Tue Jun 15, 2021 11:25 pm
by drakinite
You're welcome - that's great to hear :)

Re: Randomise Playlist

Posted: Fri Jun 18, 2021 2:40 pm
by plain
Oh Wow! This is great! Thanks drakinite! I don't know if I have the skill to do this but knowing it is possible and how to do it is going to be a really nice encouragement to try! Thanks!

Re: Randomise Playlist

Posted: Fri Jul 09, 2021 9:49 am
by telecore
Would someone be able to show me how to expand upon this code a bit (?): I am trying to re-write a MM4 VBS script that I developed which does the following:

(1) create a new non-auto playlist with the same name (+ "*" at the end), in the same location as the specified playlist (either auto or non-auto) - (I have this part basically working)
(2) iterate through the tracklist of the specified playlist and copy tracks determined to be unique (non-duplicate) per a programmatic criteria to the new playlist (cannot seem to do this)
(3) Note: the criteria for uniqueness is basically that the artist and title strings are different from all other tracks in the playlist, however, this will be expanded upon programmatically for some string cleanup prior to comparison and a selection criteria would eventually be added to select the highest bitrate version of any duplicated tracks for inclusion in the new playlist

I use this script all the time on auto-playlists that contain a lot of duplicates in MM4 but I am having difficulties getting some basic things to work in this new environment

Randomize stattic playlist

Posted: Sat Sep 18, 2021 6:58 am
by shani
In version 4 there was a script that randomize a static playlist. is There something familiar in version 5?

Re: Randomise Playlist

Posted: Mon Oct 11, 2021 10:58 pm
by drakinite
I've now put the script up on the site as a sample addon here: https://www.mediamonkey.com/addons/brow ... laylist-1/ :slight_smile:

Re: Randomise Playlist

Posted: Tue Nov 16, 2021 9:44 am
by Poobslag
As an alternative way to randomize a playlist without a script:

1. Right-click your playlist, and select "Play Shuffled -> Play Now". (The playlist will play in a random order.)
2. Delete the contents of your playlist. (The playlist is now empty.)
3. In the "Playing" panel, press "CTRL + A" and "CTRL + C" to copy all playing items.
4. Right-click your playlist and select "Paste". (The playlist is now in random order.)