saveToFileFolder

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

saveToFileFolder

Post by MPG »

Hi,
I am building my last add-on. this add-on will remove all of the album arts, except the first one. It will set embed the first one and set it as the front cover.
When I attempt to save the first cover so that it can be added later, the following saves the image, however, it also adds it to the image list. When I go to remove all of the images, it's not being picked up in the count. Is this the expected behavior? If so, is there a way to refresh the itmRec to pick up?

trackList.forEach(function (itmRec){
itmRec.coverList.locked(function (){
cvr = itmRec.coverList.getValue(0);
cvr.saveToFileFolder(itmRec.path, "fixAlbumArtTemp.jpg", false);
});

itmRec.coverList.locked(function (){
for(intCnt=0; intCnt<=itmRec.coverList.count; intCnt++){
cvr = itmRec.coverList.getValue(intCnt);
itmRec.coverList.removeAsync(cvr);
}
});

});
TIA
MPG
Triumph - Hold On: Music holds the secret, to know it can make you whole.
Ludek
Posts: 4959
Joined: Fri Mar 09, 2007 9:00 am

Re: saveToFileFolder

Post by Ludek »

Why you are saving the first cover to add it later?
It should be possible just change the cover storage from the file to tag.

Something like this:

Code: Select all

var coverList = track.loadCoverListAsync();
coverList.whenLoaded(()=> {
	coverList.modifyAsync(() => {  
		for(i=coverList-1; i>=0; i--){
			if (i > 0)
				coverList.delete(i);
			else
				coverList.getValue(0).coverStorage = 0; // move from file to tag if not already
		}
		// track.saveCoverListAsync(); // probably not needed, is part of track.commitAsync(); below
		track.commitAsync(); // commits and writes to tag & DB
	});
});
Note that I haven't tested the code, just a snippet that should work IMO
MPG
Posts: 418
Joined: Tue May 13, 2008 11:22 pm

Re: saveToFileFolder

Post by MPG »

Sweet! TY Ludek. I will play with your snippet and let you know the results!
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: saveToFileFolder

Post by drakinite »

MPG wrote: Tue Dec 14, 2021 3:25 pm I am building my last add-on.
I hope it's not your last add-on ever - It's great having you make addons and helping increase the versatility of how people can use MediaMonkey. :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: saveToFileFolder

Post by MPG »

Hi,
I tried for a couple of hours to get the following code to work, however, I keep having problems. The last section is based off Ludek's suggestion and I end up with one of the following problems:
1) if I leave the whenloaded as suggested, the code never falls into the modifyAsync.
2) if I remove the whenloaded, it works just fine sometimes. Sometimes MM just hangs
3) If one of the covers is a link to a file and the file is deleted from the hard drive (not through the MM interface), MM hangs.

When MM hangs, I've noticed that if I attempt to close MM by clicking the "X" on the image in the taskbar, the track property window shows up. So it's like MM is waiting for a response to the property window, but I can't get focus to it.

Any help you can provide, I'd appreciate it.

Code: Select all

function removeAll() {
	var blnFrontFound = false;
	var intCnt = 0;
	var cvr;
	var cvrList;

	trackList.forEach(function (track){
		track.coverList.locked(function () {

			//find first cover identified as the front (if any)
			for(intCnt=0; intCnt < track.coverList.count; intCnt++){
				cvr = track.coverList.getValue(intCnt);
				if( cvr.coverTypeDesc.toLowerCase() == "cover (front)"){
					blnFrontFound = true;
					intCnt = track.coverList.count + 1;
				}
			};
		
			//if any cover is embedded, make it the front
			if (blnFrontFound == false){
				for(intCnt=0; intCnt < track.coverList.count; intCnt++){
					if(track.coverList.getValue(intCnt).coverStorage = 0){
						cvr = track.coverList.getValue(intCnt);						
						cvr.coverTypeDesc = "Cover (front)";
						blnFrontFound = true;
						intCnt = track.coverList.count + 1;
					}
				};
			}

			//if none found, make first image the front
			if (blnFrontFound == false){
				cvr = track.coverList.getValue(0);
				cvr.coverTypeDesc = "Cover (front)";
			}

			cvrList = track.loadCoverListAsync();
//			cvrList.whenLoaded(() => {
				alert("h")
				cvrList.modifyAsync(() => {
					for(intCnt = cvrList.count-1; intCnt >= 0; intCnt--){
						cvr = cvrList.getValue(intCnt);
						alert(intCnt)
						//delete all covers except for front cover
						if( cvr.coverTypeDesc.toLowerCase() == "cover (front)"){
							alert("embed")
							//embed cover if not already
							if (cvrList.getValue(intCnt).coverStorage != 0){
								cvrList.getValue(intCnt).coverStorage = 0;
							}
						} else{
							alert("delete")
							cvrList.delete(intCnt);
						}
					}
				});
//			});
		});
		track.saveCoverListAsync;
		track.commitAsync;
	});
}
TIA
MPG
Triumph - Hold On: Music holds the secret, to know it can make you whole.
Ludek
Posts: 4959
Joined: Fri Mar 09, 2007 9:00 am

Re: saveToFileFolder

Post by Ludek »

Isn't the problem that you are using track.loadConverListAsync inside of
track.coverList.locked(function () {
?

I suppose that mainly using list.modifyAsync() inside of list.locked() should result in "Write lock after read lock" assertion?
Are you using debug build?

I think you should load the coverList at first and do everything in the "write lock"
i.e. something like this:

Code: Select all


trackList.forEach(function (track){
	var cvrList = track.loadCoverListAsync();
	cvrList.whenLoaded(() => {	
		cvrList.modifyAsync(() => {
				
			// and do everything here in the write lock:
				
				
		
			//find first cover identified as the front (if any)
			for(intCnt=0; intCnt < track.coverList.count; intCnt++){
				cvr = track.coverList.getValue(intCnt);
				if( cvr.coverTypeDesc.toLowerCase() == "cover (front)"){
					blnFrontFound = true;
		
					....
					....
				

And btw I think that track.saveCoverListAsync; shouldn't be needed once track.commitAsync(); is called.
If just track.commitAsync(); won't work then try rather :

Code: Select all

track.saveCoverListAsync().then( track.commitAsync);
MPG
Posts: 418
Joined: Tue May 13, 2008 11:22 pm

Re: saveToFileFolder

Post by MPG »

Thanks Ludek.
I tried:

Code: Select all

function removeAll() {
	var blnFrontFound = false;
	var intCnt = 0;
	var cvr;
	var cvrList;

	trackList.forEach(function (track){
		cvrList = track.loadCoverListAsync();	
		cvrList.whenLoaded(() => {
			alert("h")
			track.coverList.locked(function () {
alert("h") is never executed.
TIA
MPG
Triumph - Hold On: Music holds the secret, to know it can make you whole.
Ludek
Posts: 4959
Joined: Fri Mar 09, 2007 9:00 am

Re: saveToFileFolder

Post by Ludek »

Damn, my bad.

Try this:

Code: Select all

cvrList.whenLoaded().then(() => {
			alert("h");
			
Sorry for the typo
MPG
Posts: 418
Joined: Tue May 13, 2008 11:22 pm

Re: saveToFileFolder

Post by MPG »

Back to the drawing board. It generates an error:
cvrList.whenLoaded.then is not a function.
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: saveToFileFolder

Post by drakinite »

Use cvrList.whenLoaded().then(callback)

The whenLoaded() returns a Promise.*

*(it's not technically a native JavaScript promise, but you can treat it like one.)
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