Select-all does not work as intended
Hello,
In a simple plugin (https://github.com/PeterWurmsdobler/musescore/tree/main/simplicior) I would like to apply a function on a not to a selection or all if nothing had been selected. A recurring piece of code in plugin example is:
function applyToNotesInSelection(func) {
var fullScore = !curScore.selection.elements.length;
if (fullScore) {
cmd("select-all");
}
curScore.startCmd();
for (var i in curScore.selection.elements)
if (curScore.selection.elements[i].pitch)
func(curScore.selection.elements[i]);
curScore.endCmd();
if (fullScore) {
cmd("escape");
}
}
This function is kept in src/helpers.js
and called from the main qml script that defines the plugin as well as the function to be performed. Unfortunately, the mechanism only works if all is selected in advance (in Musescore 4.4.4 on Ubuntu 20.04,). I am sure I must be missing something simple.
Kind regards,
peter
Comments
I don't know the answer. (I'm far more of a beginner at this than you are! :-) However, I recall seeing some code in one of the plugins that I was looking at that trying to sort out how to use them. Sadly, I can't recall which one it was in.
What I do recall was that there were three options that had to be dealt with. The "fullScore" option implied that the entire score was already selected. The code also had to test whether nothing was selected. Something along the lines of if "BeginSelection" = "EndSelection".
Hope that helps?
Maybe if(fullScore == 0) since it will be a numeric variable rather than boolean.
I use this to detect a selection:
var nofSel = curScore.selection.elements.length;
if (nofSel > 0) {
:
}
When there is a selection you can then differentiate between a range selection and a selection of individual elements, (if you need to), by checking the boolean selection property isRange.
https://musescore.github.io/MuseScore_PluginAPI_Docs/plugins/html/class…
There is also a selection.clear() method that can be used instead of cmd('escape') .
In reply to Maybe if(full-scale == 0)… by yonah_ag
Hello, many thanks for your patience and help.
I have tried several things and I think I was barking up the wrong tree. In fact, the debug log says upon calling
cmd("select-all");
:Does it matter from what file the
cmd
is called from?Cheers, peter.
In reply to Hello, many thanks for your… by Peter Wurmsdobler
Maybe it does matter, or maybe it's not yet part of the MS4 plugin api. I've only used it with MS3 and within a single qml file.
An update, even if in
MuseScore-Studio-4.5.1.250800846
thecmd("select-all")
may not work, can the direct call toselectRange
be expected to work, as suggested in https://musescore.org/en/node/320673 ?