Accessing constants, etc. from another Addon

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

Moderators: jiri, drakinite, Addon Administrators

MyVikes
Posts: 91
Joined: Sun Jul 02, 2017 1:20 am

Accessing constants, etc. from another Addon

Post by MyVikes »

I've written a couple of Addons and now I'd like to access some of the constants, etc. from one within a second so that I don't need to copy/replicate the code between them.

The second Addon is a dialog Addon and otherwise works just fine. What I've been able to do is in the second Addon access these from the init and local js files but using the same code in my dlg Addon js file these are "not defined". I've experimented using requirejs and I get no errors on this command but still get the undefined on the constant.

I can also access the MM5 Utils methods for example so is this even possible and if so what might I be doing wrong?
drakinite
Posts: 965
Joined: Tue May 12, 2020 10:06 am
Contact:

Re: Accessing constants, etc. from another Addon

Post by drakinite »

IIRC, local addon scripts are run inside an anonymous function, to avoid potential issues with name clashes of local variables. To get around this & define variables you *want* to be accessible from other scripts, simply define them under the window object. For example:

Code: Select all

window.MY_CONSTANT = 3.1415;
If you want to access the constants from other dialogs, do:

Code: Select all

app.dialogs.getMainWindow().getValue('MY_CONSTANT')
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.
MyVikes
Posts: 91
Joined: Sun Jul 02, 2017 1:20 am

Re: Accessing constants, etc. from another Addon

Post by MyVikes »

Thx drakinite...i am able to make what you're describing work for a simple constant but i'm trying to reuse functions as follows:

My "const" with a function in my first Addon is defined as:

Code: Select all

const MM5SERVICES = {
    NowFormatted(inDaysBack) {
        ......
        return nowFormatted;
    },
    PadNumber(num, size) {
        ......
        return num;
    },    ......
};
in my local.js i do:

Code: Select all

window.MM55 = MM5SERVICES ;
thus thinking in my dlg...js file i can get access to it with:

Code: Select all

const MM5SERVICESDLG = app.dialogs.getMainWindow().getValue('MM5');
MM5SERVICESDLG.NowFormatted(360));
but i'm still getting undefined on NowFormatted.

Should this be possible?
drakinite
Posts: 965
Joined: Tue May 12, 2020 10:06 am
Contact:

Re: Accessing constants, etc. from another Addon

Post by drakinite »

I'm having no issue with that use case.

Main window:

Code: Select all

(() => {
    var testObj = {
        foo: (num) => {
            return num + 1;
        },
        bar: 1
    }
    window.test = testObj;
})();
Dialog:

Code: Select all

var test = app.dialogs.getMainWindow().getValue('test');
test.foo(1); // returns 2
MyVikes wrote: Sun Jun 12, 2022 7:42 pm
in my local.js i do:

Code: Select all

window.MM55 = MM5SERVICES ;
thus thinking in my dlg...js file i can get access to it with:

Code: Select all

const MM5SERVICESDLG = app.dialogs.getMainWindow().getValue('MM5');
MM5SERVICESDLG.NowFormatted(360));
in the code snippets, you are doing window.MM55 but getValue('MM5')... was that a typo in your post or is that actually in your code?
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.
MyVikes
Posts: 91
Joined: Sun Jul 02, 2017 1:20 am

Re: Accessing constants, etc. from another Addon

Post by MyVikes »

HA...it was a mistake in my code...the MM55 should have been MM5.

Once again...thx so much for an app that is a go-to app for me and i'm sure so many others.
drakinite
Posts: 965
Joined: Tue May 12, 2020 10:06 am
Contact:

Re: Accessing constants, etc. from another Addon

Post by drakinite »

No problem, happy to help. :slight_smile: Have you published any of the addons you've written? I don't think I've seen any submissions from you on our addon site (https://www.mediamonkey.com/addon_system/admin/)
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.
MyVikes
Posts: 91
Joined: Sun Jul 02, 2017 1:20 am

Re: Accessing constants, etc. from another Addon

Post by MyVikes »

I have not published any scripts to date. My scripts are intended to support my highly customized way in which i listen to music which is quite large and diverse. The bulk of my scripts are to support the Android app i've written to support remote control (which of course is not so much a unique need) but also filtering and selecting music in a way so as not repeat albums except in a "recently added" 90-day time window which are in a heavy rotation. This app also allows me to browse my collection, control the output target (i.e. internal and chromecast clients) as well as the volume,

Early on i had a React front end (mostly for learning purposes) when my "scripts" were actually a Windows app connecting via COM to MM4 and serving up web services but i found it tedious to maintain both and dropped development of that. Not sure what population of users are on Android that this would be of value to publish. And besides i'd still have to publish the Android app which i had done 20+ years ago for another app i'd written to largely do the same for the music stored on my phone and hence the inspiration to write the MM remote control app in the first place. I still use that app heavily when off my home network but it's no longer available on the Google app store.

What i'm working on now are scripts to add albums to a MM5 playlist (largely now done) i use to sync music to my phone and mark them as played. I'd written an entire app to do this, including the downloading capability, but you all made that irrelevant with the sync'ing capabilities of your Android app. :-)

Javascipt is not my "first" language so although the code is largely "bug" free (well it doesn't crash for it's intended purpose) it's still rough but improving. There were lots of challenges and the biggest one was probably figuring out how to serve up the binary album art via Javascript but it's been fun figuring it out.

Pretty long-winded explanation for saying it's largely a hobby for me now but maybe someday i'll publish some of my scripts along with the Android app of course.

Thanks again for your help.
Post Reply