Controlling MM5 from External Applications

From MediaMonkey Wiki
Jump to navigation Jump to search

Cross-Platform

A cross-platform way of controlling MM5 from external applications is via the Chrome Devtools API. You can find an explanation on how to use it with MediaMonkey here: https://www.mediamonkey.com/forum/viewtopic.php?p=447737#p447737

Windows Only

In addition to scripts that can be installed and run from within the application, MediaMonkey 5 exposes an API via the Microsoft COM model. This allows external applications to access and control MediaMonkey directly. These applications can be written in any language that can access COM objects. VBScript can be used to produce external scripts, but regular Visual Basic (or any COM-aware language) can be used as well, allowing for applications to gain access to MediaMonkey 5 without users needing to install addons.

***NOTE***: The API that MediaMonkey 5 exposes is much more limited in scope than MediaMonkey 4. All UI-related methods have been deprecated, either removed completely or just not implemented. The only ones that remain are involved with back-end operations, such as playback, playlists, and the database.


In order to work with MediaMonkey 5's objects from an external script or application, you need to create a reference to the SDB object (short for SongsDB, an instance of the SDBApplication class) yourself. For .NET-based applications, you must also add a reference to MediaMonkey's COM object "MediaMonkey Library" to your Visual Studio project, so that the compiler can find MediaMonkey and the SongsDB5 namespace.

The SDBApplication class is largely unchanged from MediaMonkey 4, but the biggest difference is that it is named SongsDB5.SDBApplication instead of SongsDB.SDBApplication. If MediaMonkey is not running yet, it will be started and a reference will be returned. In addition, if MediaMonkey was not running before, it will normally shut down automatically when the external script/application exits. This can be prevented (so MediaMonkey will remain running) by setting SDB's ShutdownAfterDisconnect property to False.

Running JS from your external application

In MediaMonkey 5.0+, SongsDB5.SDBApplication has a new method runJSCode which allows the calling application to run any JavaScript code. It is executed in the main window, so the code will have access to all

To pass data from JS back to your application, you can use the globally-accessible method runJSCode_callback. It accepts a single string. If you choose to run runJSCode_callback, the calling application will receive that string as a result from runJSCode. Example:

Dim SDB : Set SDB = CreateObject("SongsDB5.SDBApplication")
Dim SLText : SLText = SDB.runJSCode("(()=>{runJSCode_callback('This was sent by a VB Script!!!');})();", True)
MsgBox(SLText)

This will open a MsgBox with the text "This was sent by a VB Script!!!".

Dim SDB : Set SDB = CreateObject("SongsDB5.SDBApplication")
Dim SLText : SLText = SDB.runJSCode("app.playlists.getByTitleAsync('Test Playlist').then(function(playlist) { if (!playlist) runJSCode_callback('Could not find playlist'); else playlist.getTracklist().whenLoaded().then(function (list) { runJSCode_callback(list.asJSON); }); });", True)

MsgBox(SLText)

This will open a MsgBox with the contents of the playlist titled "Test Playlist", in JSON format.

For readability, here is the above JS code when pretty-printed:

app.playlists.getByTitleAsync('Test Playlist')
    .then(function(playlist) { 
        if (!playlist) 
            runJSCode_callback('Could not find playlist'); 
        else 
            playlist.getTracklist()
                .whenLoaded()
                .then(function (list) { 
                    runJSCode_callback(list.asJSON); 
                }); 
    });