Changing a barline type via code

• Mar 3, 2022 - 09:57

Is it possible to run a plugin that would change the currently selected barline? if not, is it possible to change the currently selected bar's barlines?


var segment = measure.lastSegment;
var bar = segment.elementAt(0);
if (bar.type == Element.BAR_LINE) {
bar.barlineType = 32;
console.log("Last element is a barline (type=" + bar.userName() + ")");
} else {
console.log("Last element is not a barline (type=" + bar.userName() + ")");

Can you explain what you are trying to do in more detail? You don't need to run a plugin just to change barlines; the Barlines palette does this directly.

In reply to by Marc Sabatella

I have some workflow that I want to optimize/automate where I change the barline types based on the workflow. My current issue is: "selecting a bar or a note in a bar, and running the plugin, would change the current bar starting barline to START_REPEAT" but it doesn't always work

This is the code I have so far, which doesn't always work

onRun: {
var selected = curScore.selection.elements[0];

             if (selected.type !== Element.NOTE){
                       selected = curScore.selection.elements[1];

    var cursor = curScore.newCursor();
            cursor.inputStateMode = "INPUT_STATE_SYNC_WITH_SCORE";

           var segment = selected.parent;
    while (segment.type !== Element.SEGMENT) {
        segment = segment.parent;
    var tick = segment.tick;


    cursor.filter = Segment.BarLineType;

            cursor.element.barlineType = 4



In reply to by Guyis

I'm afraid there is bug in MuseScore.
Check my code.
If you play it for changing the bar to regular/double/end repeat it works fine, the right bar is changed.
If you play it for changing the bar to start repeat, then the next bar is changed.

Play it on a note of the 1st, 2nd and 3rd measure to observe the difference of behaviour.

You could try to workaround it by selecting the previous bar, but this will be limited. I guess you won't be able to change the bar between the 1st and 2nd measure, because there is no previous bar to select.

Attachment Size
mcve.mscz 2.86 KB
mcveChangeBarLine.qml 1.69 KB

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