curScore.selection.elements.length always returns 0
Hi there
I'm trying to enumerate through elements in my score, but the following code (where I select all, get the list of elements in the selection) always returns 0, even if I have notes and text in my score:
curScore.startCmd()
curScore.selection.selectRange(0,curScore.lastSegment.tick + 1,0,numStaves);
var elems = curScore.selection.elements;
dialog.msg += "\nFound "+elems.length+" elems"; // Always returns 0
curScore.endCmd()
Any thoughts?
Comments
Try
curScore.startCmd()
curScore.selection.selectRange(0,curScore.lastSegment.tick + 1,0,curScore.nstaves);
curScore.endCmd()
and then
var elems...
In reply to Try curScore.startCmd()… by elsewhere
Thank you! That worked!
Is there a reason for that though? I'm guessing from this that I should only use startCmd()/endCmd() when I'm altering something on the score — is that documented somewhere?
In reply to Thank you! That worked! Is… by michaelnorris
See https://musescore.org/en/node/334669
EDIT: In other words: if you don't put in endcmd, the code runs ahead before the selection command is completed, and does not see anything selected yet...