How to copy selections so they go one after another

• Feb 4, 2024 - 15:49

Hi everyone,

As shown in the picture, I simply want to randomly select rehearsal mark sections and copy them to the track below. It works, but each time I have to manually select those rehearsal mark sections and delete the generated part in the track below before I can get a new set of random rehearsal mark sections.

I want these sections to stack one after another. How can I achieve this? Even if I save the end of the generated section, I have to run the plugin again, which deletes the saved data, and it cannot paste after the already inserted section.

Thank you for your help!

import QtQuick 2.0
import MuseScore 3.0

MuseScore {
menuPath: "Plugins.pluginName"
description: "Description goes here"
version: "1.0"

onRun: {
    var cursor = curScore.newCursor();
    cursor.rewind(2);
    var endTick = cursor.tick;

    var rehearsalTick = [];

    function getRandRange(numOfMarks) {
        var randNum = Math.floor(Math.random() * numOfMarks);

        return { firstBoundry: rehearsalTick[randNum], secondBoundry: rehearsalTick[randNum + 1] };
    }

    cursor.rewind(1);
    var allElements = curScore.selection.elements;

    for (var i = 0; i < allElements.length; i++) {
        if (allElements[i].type === 44) {
            rehearsalTick.push(allElements[i].parent.tick);
        }
    }

    rehearsalTick.push(endTick);

    var finalRange = getRandRange(rehearsalTick.length - 1);

    console.log("first", finalRange.firstBoundry);
    console.log("second", finalRange.secondBoundry);

    curScore.startCmd();
    curScore.selection.selectRange(finalRange.firstBoundry, finalRange.secondBoundry, 0, 0);
    curScore.endCmd();

    cmd("copy");

    cursor.track  = 4;

    curScore.selection.select(cursor.element);
    cmd("paste");
}

}

Attachment Size
diffParts.png 39.48 KB

Comments

> but each time I have to manually select those rehearsal mark sections and delete the generated part in the track below before I can get a new set of random rehearsal mark sections.

Why do you have to do this?

In reply to by kamilio141414

It isn't entirely clear to me what you're desiring to achieve based on your words, unfortunately, so it isn't clear for me to see what I should expect in attempting anything.

1) Manually create range selection apparently is required according to your code: var allElements = curScore.selection.elements;

2) Running the thing produces a copy below to the next bottom staff. Not really "next" but hard coded, as you have track = 4 which is the 0th voice of the next staff (first staff is 0-3)

3) Current range selection at this stage is on the below staff of the resulting paste. If you run the plugin again, what do you expect to happen? What would you like to happen?

In reply to by worldwideweary

I am sorry english is not my first language.

What I trying to do is to randomly select range of music which a "divided" by rehearsal marks, as shown in the picture. Then paste it somewhere else. Then I want the reselect the same range of music and randomly select different section (either A, B, or C) and paste it after the the section that has been randomly selected in the first step.

This way I want to randomly paste different section of rehearsal one after another creating different melodies.

In reply to by kamilio141414

I see, yeah you'll have a more work to do in your QML compared to what's showing in your initial code. If you want to be able to manually do this instead of have it happen in one process, you should probably make a graphical interface with some buttons to press that will "continue" on command so that you can retain your ranges of what was copied and where it was pasted. Browsing the code in the available plugins list could definitely help!
G'luck

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