Setting Field Value to null

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

Moderators: jiri, drakinite, Addon Administrators

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

Setting Field Value to null

Post by MPG »

Hi,
I'm an old vba programmer and trying to learn this who JavaScript language. Thought I'd trying something really easy to start with by setting some fields to null or an empty string.

I have the following code:
actions.ClearFields = {
title: _('Clear Fields'),
hotkeyAble: true,
disabled: uitools.notMediaListSelected,
visible: window.uitools.getCanEdit,
execute: async function () {

list = await uitools.getSelectedTracklist().whenLoaded();
list.forEach(function (lstItem) {
if(lstItem.author !== null){
lstItem.author = null;
};
};
list.commitAsync();
};
};

I've set up alerts to confirm I am setting the value so I know that's working. but the commit isn't being saved.

Any suggestions on what I'm doing wrong?
TIA
MPG
Triumph - Hold On: Music holds the secret, to know it can make you whole.
MPG
Posts: 259
Joined: Tue May 13, 2008 11:22 pm

Re: Setting Field Value to null

Post by MPG »

I don't seem to be grabbing the value of the field. When I do an alert on the lstItem.author I am getting "undefined" so I think I am getting the field object, not the value. Any way to explicitly get the field value?
TIA
MPG
Triumph - Hold On: Music holds the secret, to know it can make you whole.
MPG
Posts: 259
Joined: Tue May 13, 2008 11:22 pm

Re: Setting Field Value to null

Post by MPG »

I am making headway....
I can get the value of all the fields except the following:
Disc Number
Original Date
Comment
Grouping
Involved People
Original Artist
Original Title
Original Lyricist
Lyrics

Does anyone know what the field names are in the lstItem object?
TIA
MPG
Triumph - Hold On: Music holds the secret, to know it can make you whole.
drakinite
Posts: 563
Joined: Tue May 12, 2020 10:06 am
Contact:

Re: Setting Field Value to null

Post by drakinite »

I see two issues with the original code you posted:
1. There's no closing parenthesis on your forEach
and 2. adding a semicolon at the end of your execute function, in this case, is invalid (because it's an object property)

So in this case, the syntactically valid code would be:

Code: Select all

actions.ClearFields = {
	title: _('Clear Fields'),
	hotkeyAble: true,
	disabled: uitools.notMediaListSelected,
	visible: window.uitools.getCanEdit,
	execute: async function () {

		list = await uitools.getSelectedTracklist().whenLoaded();
		list.forEach(function (lstItem) {
			if (lstItem.author !== null) {
				lstItem.author = null;
			};
		});
		list.commitAsync();
	}
};
But also, the reason it's not working is because the type you're setting the properties to is incorrect. For string attributes (artist, lyrics, author, people, title, etc.) the correct "null" value is an empty string ( "" ). For numerical values (date, bpm, bps, year, etc.), the correct "null" value is -1. For reference, you can view all the appropriate properties and their "null" values by creating an empty track:

Code: Select all

myEmptyTrack = app.utils.createEmptyTrack();
You can easily view all its properties if you create it in the devtools console.

edit: Just to be clear, when I'm saying 'the correct "null" value' I'm not talking about the value that a track takes when that value is not set. Not the JavaScript literal null.
Image
Student electrical-computer engineer, web programmer, part-time MediaMonkey developer, full-time MediaMonkey enthusiast
MPG
Posts: 259
Joined: Tue May 13, 2008 11:22 pm

Re: Setting Field Value to null

Post by MPG »

Thanks draknite.

what is this devtools that you mention? Where do I get it or enable it?
TIA
MPG
Triumph - Hold On: Music holds the secret, to know it can make you whole.
MPG
Posts: 259
Joined: Tue May 13, 2008 11:22 pm

Re: Setting Field Value to null

Post by MPG »

I found it. Yea me. I'm off to the races. Thanks for your help!
TIA
MPG
Triumph - Hold On: Music holds the secret, to know it can make you whole.
drakinite
Posts: 563
Joined: Tue May 12, 2020 10:06 am
Contact:

Re: Setting Field Value to null

Post by drakinite »

No problem!

Since I already typed this out, I'll post it in case someone else needs it:
The easiest way is by installing a Debug build (either from the stable download page or the beta builds thread. When you are running a debug build, you can right click anywhere and click Inspect Element.

If you want to keep a non-debug build installed, you can alternatively go to http://localhost:9222 in a web browser, and click on mainwindow.html to inspect the main window. Both work, but the first option is more convenient.
Image
Student electrical-computer engineer, web programmer, part-time MediaMonkey developer, full-time MediaMonkey enthusiast
MPG
Posts: 259
Joined: Tue May 13, 2008 11:22 pm

Re: Setting Field Value to null

Post by MPG »

Okay, I can set all of the fields except for comment and lyrics. I suspect because they are long text boxes so using the standard " = " doesn't work.

e.g. lstItem.commentShort = "abcd"; fails.

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

Re: Setting Field Value to null

Post by drakinite »

I haven't looked much at commentShort, but it looks like it's probably a read-only field. Try setCommentAsync: https://www.mediamonkey.com/docs/api/cl ... mmentAsync
Image
Student electrical-computer engineer, web programmer, part-time MediaMonkey developer, full-time MediaMonkey enthusiast
MPG
Posts: 259
Joined: Tue May 13, 2008 11:22 pm

Re: Setting Field Value to null

Post by MPG »

Thanks again!
setCommentAsync works!
TIA
MPG
Triumph - Hold On: Music holds the secret, to know it can make you whole.
drakinite
Posts: 563
Joined: Tue May 12, 2020 10:06 am
Contact:

Re: Setting Field Value to null

Post by drakinite »

Great! No problem.

I'll update the documentation with that info soon.
Image
Student electrical-computer engineer, web programmer, part-time MediaMonkey developer, full-time MediaMonkey enthusiast
Post Reply