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?
Accessing constants, etc. from another Addon
Moderators: jiri, drakinite, Addon Administrators
Re: Accessing constants, etc. from another Addon
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:
If you want to access the constants from other dialogs, do:
Code: Select all
window.MY_CONSTANT = 3.1415;
Code: Select all
app.dialogs.getMainWindow().getValue('MY_CONSTANT')

Data scientist, 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.
Re: Accessing constants, etc. from another Addon
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:
in my local.js i do:
thus thinking in my dlg...js file i can get access to it with:
but i'm still getting undefined on NowFormatted.
Should this be possible?
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;
}, ......
};
Code: Select all
window.MM55 = MM5SERVICES ;
Code: Select all
const MM5SERVICESDLG = app.dialogs.getMainWindow().getValue('MM5');
MM5SERVICESDLG.NowFormatted(360));
Should this be possible?
Re: Accessing constants, etc. from another Addon
I'm having no issue with that use case.
Main window:
Dialog:
Main window:
Code: Select all
(() => {
var testObj = {
foo: (num) => {
return num + 1;
},
bar: 1
}
window.test = testObj;
})();
Code: Select all
var test = app.dialogs.getMainWindow().getValue('test');
test.foo(1); // returns 2
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?MyVikes wrote: ↑Sun Jun 12, 2022 7:42 pm
in my local.js i do:thus thinking in my dlg...js file i can get access to it with:Code: Select all
window.MM55 = MM5SERVICES ;
Code: Select all
const MM5SERVICESDLG = app.dialogs.getMainWindow().getValue('MM5'); MM5SERVICESDLG.NowFormatted(360));

Data scientist, 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.
Re: Accessing constants, etc. from another Addon
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.
Once again...thx so much for an app that is a go-to app for me and i'm sure so many others.
Re: Accessing constants, etc. from another Addon
No problem, happy to help.
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/)

Data scientist, 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.
Re: Accessing constants, etc. from another Addon
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.
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.