Writing text to a special notehead

• Mar 16, 2016 - 10:24

I figured out, how to write a text in a score with a plugin.

import QtQuick 2.0
import MuseScore 1.0

MuseScore {
version: "2.0"
description: "None"
menuPath: "Plugins.Fingersatz"

onRun: {
var cursor = curScore.newCursor();
cursor.rewind(0);

var text = newElement(Element.TEXT);
text.text = "1";
text.color = "#ff0000";

cursor.add(text);

Qt.quit();
} // end onRun
}

But this write the text at the beginning of the score.

Q1: What commands do I need, that the anchor of the text is the note head I activated in the score.
Q2: Is there a command like text.style = RH fingering?


Comments

1) cursor.rewind(0) puts your cursor at the beginning of the score. You're likely looking for cursor.rewind(1) to end up at the start of your selection. I'm not sure if it works when you have only a notehead selected though. It does work if the note was selected with Shift-click

2) If you want to create a fingering text element, don't use Element.TEXT but rather use Element.FINGERING as the element type. I'm also unsure about this, but it could be that you'll have to add it to the annotations of the selected note/segment rather than simply at the cursor position.
You could create a simple score of 1 measure with a note with a fingering on it and then loop over that score to try and find out where the Fingering is attached to (walk.qml might help in doing so)

Chances are that the text-style will be better or correct if you use the most specific element type available. Setting the text-style is currently not possible, but a code patch to enable this is already pending (https://github.com/musescore/MuseScore/pull/2408).
Once it is in place you'll be able to write

myText.textStyleType = TextStyleType.RH_GUITAR_FINGERING;

In reply to by jeetee

1) cursor.rewind(0) puts your cursor at the beginning of the score. You're likely looking for cursor.rewind(1) to end up at the start of your selection. I'm not sure if it works when you have only a notehead selected though. It does work if the note was selected with Shift-click

Thank you, that works.

2) If you want to create a fingering text element, don't use Element.TEXT but rather use Element.FINGERING as the element type. I'm also unsure about this, but it could be that you'll have to add it to the annotations of the selected note/segment rather than simply at the cursor position.

This works not. Writing "fingering" or "Fingering" causes
Debug: cannot create type 0
14:-1: TypeError: Type error
14:-1: TypeError: Type error
14:-1: TypeError: Type error
Warning: :14: TypeError: Type error

Writing "FINGERING" causes a runtime error and musescore are finished.

In reply to by hasenfuss

The Element type has to be all in capitals to work. This works for me:

var fingering = newElement(Element.FINGERING);
console.log(fingering + ' | ' + fingering.type + ' == ' + Element.FINGERING);
fingering.text = '3';
console.log(fingering.text);

giving the following output

Debug: Ms::Fingering(0xb3d3738) 31 == 31
Debug: 3

I'm guessing your crash is caused by the call to cursor.add(). Internally it uses the undo stack to add the element. To be able to do that from within a plugin, you'll need to call curScore.startCmd(); and curScore.endCmd(); respectively before and after the cursor.add() call.

All actions you do between startCmd and endCmd will result in one 'undo-action' afterwards. So if you run for example (pseudocode):

curScore.startCmd();
for (1 -> 10) {
      cursor.add(a_new_note);
}
curScore.endCmd();

and then press the Undo shortcut (Ctrl+Z) all 10 added notes will be removed in the same undo command.
If you however would run (pseudocode):

for (1 -> 10) {
      curScore.startCmd();
      cursor.add(a_new_note);
      curScore.endCmd();
}

you'd have to trigger Undo 10 times afterwards to make all notes disappear again.

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