How do I rename supporting files when an MP3 is renamed?

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

Moderators: jiri, drakinite, Addon Administrators

variables
Posts: 2
Joined: Wed Dec 22, 2021 12:33 am

How do I rename supporting files when an MP3 is renamed?

Post by variables »

I've got a collection of mp3/cdg files i'd like to organize. When a mp3 is renamed/copied, I'd like the same operation to happen to the accompanying cdg file.

I tried making an addon with the following code:

init.js

Code: Select all

app.filesystem.override({
    copyFileAsync: function($super, src, dst) {
        $super(src, dst);
        alert(`copy ${src} to ${dst}`);
        // use fileExists and $super to copy the cdg file
    }
});
I didn't get any errors but I also didn't any alert boxes. So either my addon didn't load or autoOrganize doesn't call any of the filesystem methods.

also tried with no alert shown

Code: Select all

    app.trackOperation.override({
        autoOrganize: function($super, sl, names, isMoving, addToLib, removeEmpty) {
            alert(JSON.stringify(names));
            $super(sl, names, isMoving, addToLib, removeEmpty);
        }
    });
variables
Posts: 2
Joined: Wed Dec 22, 2021 12:33 am

Re: How do I rename supporting files when an MP3 is renamed?

Post by variables »

I've figured out accomplish my goal but it's pretty hacky.

dlgAUtoorganize_add.js

Code: Select all

function btnOkPressed() {
    if (cancellationToken) {
        requestTimeout(function () {
            btnOkPressed();
        }, 10);
        return;
    }



    var btn = UI.btnOK;
    if (btn.controlClass && !btn.controlClass.disabled && !windowHandled) {
        windowHandled = true;

        var isMoving = UI.moveFiles.controlClass.checked;
        if (isMoving)
            state.mode = 'move';
        else
            state.mode = 'copy';
        app.setValue('dlgAutoOrganize', state);
        var filesOp = UI.filesOp;
        var addToLib = false;
        var removeEmpty = false;
        if (isMoving) {
            removeEmpty = filesOp.controlClass.checked;
        } else {
            addToLib = filesOp.controlClass.checked;
        }

        var sett = window.settings.get();
        sett['Auto-organize'].DeleteEmptiedFolders = filesOp.controlClass.checked;
        window.settings.set(sett);

        // prepare list of tracks to organize and their new names
        var sl = app.utils.createTracklist();
        var names = newStringList();
        tracks.locked(function () {
            newNamesAll.locked(function () {
                var track = undefined;
                for (var i = 0; i < tracks.count; i++) {
                    if (tracks.isChecked(i)) { // track is checked                        
                        track = tracks.getFastObject(i, track);
                        var newPath = newNamesAll.getValue(i);
                        if (track.path != newPath) {
                            sl.add(track);
                            names.add(newPath);
                        }
                    }
                }
            });
        });


        if (sl.count > 0) {
            // run auto organize (it's done in background)
            app.trackOperation.autoOrganize(sl, names, isMoving, addToLib, removeEmpty);

            sl.locked(function () {
                names.locked(function() {
                    const fileCount = names.count;
                    for (let i = 0; i < fileCount; i++) {
                        const oldFilePath = sl.getValue(i).path;
                        const newFilePath = names.getValue(i);
                        const oldCdgPath = oldFilePath.replace(/.mp3$/i, '.cdg');
                        const newCdgPath = newFilePath.replace(/.mp3$/i, '.cdg');
                        
                        app.filesystem.fileExistsAsync(oldCdgPath).then((exists) => {
                            if (exists) {
                                if (isMoving) {
                                    app.filesystem.moveFileAsync(oldCdgPath, newCdgPath); 
                                } else {
                                    app.filesystem.copyFileAsync(oldCdgPath, newCdgPath);
                                }
                            }
                        });
                    }
                })
            });
        }

        app.settings.addMask2History(UI.cbMasks.controlClass.value, 'OrganizeMasks');

        closeWindow();
    }
}
I feel like I should be able to "override" the filesystem methods and app.trackOperation.autoOrganize should call those methods if they don't.

If there is a more elegant way to do this i'd love some suggestions. This makes me feel dirty.
Ludek
Posts: 4964
Joined: Fri Mar 09, 2007 9:00 am

Re: How do I rename supporting files when an MP3 is renamed?

Post by Ludek »

Hi,
I can confirm that the native methods (parts of the app obejct) can't be overriden.

So you will have to "override" this in the JS code part -- which in your example (overriding the btnOKPressed).
The cleaner solution would be for us (MM5 team) to add another method to btnOKPressed to the place where your new code resides for a future version of MM5 and your script.
Post Reply