Retrieving/passing command-line options from a plugin

• Apr 8, 2022 - 12:01

Hi,
I'm writing a command-line plugin to perform a custom export of some metadata.

Is there a way to access from the plugin the command line options, whether passed by string (e.g. "C:\Program Files\MuseScore 3\bin\MuseScore3.exe" -p mcveCmdLine.qml -o "xyz "path\to\some\file.mscz") or by Json file ?

Actually I'd like to pass to the plugin a filename where to do that export or a config file telling where to do it.

I could either retrieve the "-o" command or retrieve the "out:" param from the json.
Or use a specific tag.

Anyone with experience with this ?

Cheers


Comments

You can use Qt.application.arguments in the plugin to get a list of all command-line arguments passed to the application (see QML docs). Given that, you can search through that list for the argument your plugin needs.

From my experience there is a different issue here: MuseScore also interprets command-line arguments with its own logic — for example, the -o argument will make MuseScore attempt to save something into that file. Passing a completely custom option doesn't work as it is not permitted by the MuseScore's command line parser. The workaround I have found is using the --highlight-config option which doesn't affect MuseScore's behavior in the non-GUI mode but still makes its command line parser satisfied. So for invoking my plugins which need to take command-line arguments I use something like the following:

musescore input_score.mscz -p PluginName.qml --highlight-config 'my plugin args'

In reply to by dmitrio95

Tested with success. Thanks.

Although, the behaviour on Windows doesn't seems to be the same yours (Linux ?).

Non recognized parameters such as your --highlight-config 'my plugin args'are throwing an errors.
While recognized parameters such as a -o "path/to/some/file/" are passed to the plugin.

I combined this with a conversion extension not recognized by Musescore (such as ".tmp") so that MuseScore doesn't try to perform a conversion itself.

In short:

import QtQuick 2.0
import MuseScore 3.0
import FileIO 3.0
 
MuseScore {
    menuPath: "Plugins.pluginName"
    description: "Description goes here"
    version: "1.0"
    requiresScore: false
    onRun: {
 
        var commandLine = Qt.application.arguments;
 
        var idx = commandLine.indexOf("-o");
 
        var dest = (idx >= 0) ? commandLine[idx + 1] : null;
 
        var res = {
            "timestamp": new Date(),
            "commandline": commandLine,
            "dest": dest
        };
        console.log(JSON.stringify(res));
 
        try {
            console.log("Writing to : " + logfile.source); 
            logfile.write(JSON.stringify(res));
        } catch (e) {
            console.log("Error while witing to log file : " + e.message);
        }
 
        if (dest !== null) {
            outfile.source = dest;
            try {
                console.log("Writing to : " + outfile.source);
                outfile.write(res.timestamp);
            } catch (e) {
                console.log("Error while witing to output file : " + e.message);
            }
        }
 
        Qt.quit()
    }
 
    FileIO {
        id: logfile
        source: tempPath() + "/test.dat" // = TEMP folder
    }
 
    FileIO {
        id: outfile
    }
}

In reply to by jeetee

Probably some spelling error, because it is now working with --highlight-config. Thanks.
[OFF TOPIC]
The documentation is not super-clear (for the "--xxx" options) which of these options are taking parameters. E.g. for the "highlight-config" there is no mention of this taking a string as parameters.

Is it only me, or even a basic hello world plugin don't run on MuseScore 4.2.1?

Something with

onRun: {
console.log('running function')
}

when invoked with

mscore test.mscz --plugin test.qml --debug -o test.pdf

generate a warning 'Qt | No such plugin for spec "test.qml"'

Do you still have an unanswered question? Please log in first to post your question.