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:
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
Yep! You can find a step by step guide on how to create an addon/script here: https://www.mediamonkey.com/wiki/Getting_Started_(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]
actions.playlistRandomise = { }
[/code]
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]
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);
});
}
};
[/code]
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/Important_Methods_and_Utilities_(Addons)#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]
PlaylistHeader.prototype.override({
_initButtons: function ($super) {
$super();
// now we do our own stuff
}
});
[/code]
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]
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
});
}
});
[/code]
6) Now, pack your MMIP, install it, and test it! https://lambda.sx/IdwS.mp4