Get and Set Song Rating

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

Moderators: jiri, drakinite, Addon Administrators

MPG
Posts: 418
Joined: Tue May 13, 2008 11:22 pm

Get and Set Song Rating

Post by MPG »

Hi,
Need to update one of my scripts. I need to check the song rating and then set it if it isn't -1.

Code: Select all

        list.forEach(lstItem => {

			lstItem.beginUpdate();

			if(lstItem.rating !== -1){
				lstItem.setRating(-1, {
					force: true
				});
			};

...
I'm trying the above with no luck. I've also tried setting the value with lstItem.rating = -1; with no luck.

Any suggestions?
TIA
MPG
Triumph - Hold On: Music holds the secret, to know it can make you whole.
drakinite
Posts: 965
Joined: Tue May 12, 2020 10:06 am
Contact:

Re: Get and Set Song Rating

Post by drakinite »

Doing lstItem.rating = -1; should work. I believe your problem is doing lstItem.beginUpdate(). Doing this is not necessary for individual track items. The documentation says "Events are not called when in update state", so you might not have seen the rating change in the UI because the it was in that specific state.

I haven't used beginUpdate()/endUpdate() on tracklists myself, so I can't say for certain what the use case of it is. I don't believe you need to worry about using that method, however. Just use commitAsync() either on the individual tracks or the tracklist after you're done to commit the metadata changes.

Code: Select all

list.forEach(lstItem => {
	if(lstItem.rating !== -1){
		lstItem.rating = -1;
	}
});
list.commitAsync();
P.S. I see that the rating property isn't present on the API docs; Adding it now.
Image
Student electrical-computer engineer, web programmer, part-time MediaMonkey developer, full-time MediaMonkey enthusiast
I uploaded many addons to MM's addon page, but not all of those were created by me. "By drakinite, Submitted by drakinite" means I made it on my own time. "By Ventis Media, Inc., Submitted by drakinite" means it may have been made by me or another MediaMonkey developer, so instead of crediting/thanking me, please thank the team. You can still ask me for support on any of our addons.
MPG
Posts: 418
Joined: Tue May 13, 2008 11:22 pm

Re: Get and Set Song Rating

Post by MPG »

Thanks for the suggestion, I tried with no success.

Here's the entire function:

Code: Select all

function clearFields(){
		// Define variables
		let list = uitools.getSelectedTracklist();

		// Process all selected tracks
        if (list.count === 0) {
            messageDlg(_("Select tracks to be updated"), 'Error', ['btnOK'], {
                defaultButton: 'btnOK'
            }, function (result) {
                modalResult = 0;
            });
            return;
        };

        list.forEach(lstItem => {
			if(lstItem.albumArtist !== ""){
				lstItem.albumArtist = "";
			}
			if(lstItem.author !== ""){
				lstItem.author = "";
			};
			if(lstItem.conductor !== ""){
				lstItem.conductor = "";
			};
			if(lstItem.rating !== -1){
				lstItem.rating = -1;
			};
			if(lstItem.discNumber !== ""){
				lstItem.discNumber = "";
			};
			if(lstItem.origDate !== 0){
				lstItem.origDate = 0;
			};
			if(lstItem.lyricist !== ""){
				lstItem.lyricist = "";
			};
			if(lstItem.commentShort.length > 0){
				lstItem.setCommentAsync("");
			};
			if(lstItem.groupDesc !== ""){
				lstItem.groupDesc = "";
			};
			if(lstItem.bpm !== -1){
				lstItem.bpm = -1;
			};
			if(lstItem.involvedPeople !== ""){
				lstItem.involvedPeople = "";
			};
			if(lstItem.origArtist !== ""){
				lstItem.origArtist = "";
			};
			if(lstItem.origTitle !== ""){
				lstItem.origTitle = "";
			};
			if(lstItem.origLyricist !== ""){
				lstItem.origLyricist = "";
			};
			if(lstItem.isrc !== ""){
				lstItem.isrc = "";
			};
			if(lstItem.publisher !== ""){
				lstItem.publisher = "";
			};
			if(lstItem.encoder !== ""){
				lstItem.encoder = "";
			};
			if(lstItem.copyright !== ""){
				lstItem.copyright = "";
			};
			if(lstItem.tempo !== ""){
				lstItem.tempo = "";
			};
			if(lstItem.occasion !== ""){
				lstItem.occasion = "";
			};
			if(lstItem.mood !== ""){
				lstItem.mood = "";
			};
			if(lstItem.quality !== ""){
				lstItem.quality = "";
			};
			if(lstItem.initialKey !== ""){
				lstItem.initialKey = "";
			};
			if(lstItem.custom1 !== ""){
				lstItem.custom1 = "";
			};
			if(lstItem.custom2 !== ""){
				lstItem.custom2 = "";
			};
			if(lstItem.custom3 !== ""){
				lstItem.custom3 = "";
			};
			if(lstItem.custom4 !== ""){
				lstItem.custom4 = "";
			};
			if(lstItem.custom5 !== ""){
				lstItem.custom5 = "";
			};
			if(lstItem.custom6 !== ""){
				lstItem.custom6 = "";
			};
			if(lstItem.custom7 !== ""){
				lstItem.custom7 = "";
			};
			if(lstItem.custom8 !== ""){
				lstItem.custom8 = "";
			};
			if(lstItem.custom9 !== ""){
				lstItem.custom9 = "";
			};
			if(lstItem.custom10 !== ""){
				lstItem.custom10 = "";
			};
			if(lstItem.lyricsShort.length > 0){
				lstItem.setLyricsAsync("");
			};
		});

		// Write back to DB and update tags
		list.commitAsync();
		
		messageDlg(_('Fields are cleared.'), 'Information', ['btnOK'], {
			defaultButton: 'btnOK'
		}, undefined);
}
Am I missing something?
TIA
MPG
Triumph - Hold On: Music holds the secret, to know it can make you whole.
drakinite
Posts: 965
Joined: Tue May 12, 2020 10:06 am
Contact:

Re: Get and Set Song Rating

Post by drakinite »

Yes; you need to asynchronously wait for the list to load via whenLoaded(). I wrote some extra documentation on it and it should be up on the API docs site soon. In the meantime:

Tracklist class (list of Tracks). In most cases, when you request a new Tracklist (for example, with uitools.getSelectedTracklist() or playlist.getTracklist()), the list will be returned before it has been populated with its contents. To resolve this, you must asynchronously wait for it to be loaded. You can do this either with Promise.then() or with async/await:

Code: Select all

// Method 1
var list = uitools.getSelectedTracklist();
list.whenLoaded()
  .then(function () {
    // do your operations on the list
  });

// Method 2
async function myFunc() {
  var list = uitools.getSelectedTracklist();
  await list.whenLoaded();
  // do your operations on the list
}
myFunc();
if you change your function definition to async function clearFields(){ and add await list.whenLoaded(); after let list = uitools.getSelectedTracklist();, it'll work.
Image
Student electrical-computer engineer, web programmer, part-time MediaMonkey developer, full-time MediaMonkey enthusiast
I uploaded many addons to MM's addon page, but not all of those were created by me. "By drakinite, Submitted by drakinite" means I made it on my own time. "By Ventis Media, Inc., Submitted by drakinite" means it may have been made by me or another MediaMonkey developer, so instead of crediting/thanking me, please thank the team. You can still ask me for support on any of our addons.
MPG
Posts: 418
Joined: Tue May 13, 2008 11:22 pm

Re: Get and Set Song Rating

Post by MPG »

At first my change didn't work, so I opted to re-install it and it worked. Thanks for all your help!
TIA
MPG
Triumph - Hold On: Music holds the secret, to know it can make you whole.
drakinite
Posts: 965
Joined: Tue May 12, 2020 10:06 am
Contact:

Re: Get and Set Song Rating

Post by drakinite »

No problem :slight_smile:
Image
Student electrical-computer engineer, web programmer, part-time MediaMonkey developer, full-time MediaMonkey enthusiast
I uploaded many addons to MM's addon page, but not all of those were created by me. "By drakinite, Submitted by drakinite" means I made it on my own time. "By Ventis Media, Inc., Submitted by drakinite" means it may have been made by me or another MediaMonkey developer, so instead of crediting/thanking me, please thank the team. You can still ask me for support on any of our addons.
MPG
Posts: 418
Joined: Tue May 13, 2008 11:22 pm

Re: Get and Set Song Rating

Post by MPG »

Just in case anyone else runs into this. I am doing mass updates to my collection. Doing method one will work for a selection of 1000 songs or less. Method two seems to have an unlimited amount.
TIA
MPG
Triumph - Hold On: Music holds the secret, to know it can make you whole.
drakinite
Posts: 965
Joined: Tue May 12, 2020 10:06 am
Contact:

Re: Get and Set Song Rating

Post by drakinite »

Since you've already done all the work for it, maybe some would appreciate if you made a "Clear All Fields" addon. :slight_smile:
Edit tags > Clear all fields; perhaps the icon could be "warning"; and make sure to make a confirmation prompt. "Are you sure you want to clear ALL METADATA on %s tracks?" (You can do this with the messageDlg global method; check the wiki for details on how to use it). I'd also make the default button "no"/"cancel", just to be safe.
Image
Student electrical-computer engineer, web programmer, part-time MediaMonkey developer, full-time MediaMonkey enthusiast
I uploaded many addons to MM's addon page, but not all of those were created by me. "By drakinite, Submitted by drakinite" means I made it on my own time. "By Ventis Media, Inc., Submitted by drakinite" means it may have been made by me or another MediaMonkey developer, so instead of crediting/thanking me, please thank the team. You can still ask me for support on any of our addons.
Post Reply