selections and cursor.rewind

• Jan 29, 2022 - 14:48


I want to select some notes, and then examine the segments in the measures these notes belong to in a plugin. (ultimite goal is to delete some rests generated by changing the voice of a note).
But trying to do this raises a lot of questions.

  1. Can this be done by iterating through segments, without using a cursor? (both objects have prev and next functions or properties) ?

  2. If you select some notes randomly (by clicking on the noteheads), are the elements in curScore.selection.elements ordered in some way? f.i. in order of their selection, or sorted by their position?

  3. If I have an element (from the curScore.selection.elements list), How can I get to the segment this element belongs to? Or to obtain the tick of the element?

  4. The documentation at… says: A segment contains a list of elements that occur at that time position across all staves.
    The segment object has an annotations list, but not an element list. It does have an elementAt() function.
    So is it correct to conclude that 'A segment contains a list of elements' means that it can 'contain' one element for each track?

  5. cursor.rewind(1) rewinds the cursor to the beginning of a selection, so is it correct that after rewind(1), cursor.segment contains the segment the first selected element belongs to?

  6. Does cursor.rewind(1) is supposed to work only for range selections?
    I executed:
    var start_seg = cursor.segment;
    console.log('cursor', cursor, 'segment', start_seg, 'tick', cursor.tick);

When I select a range with one note (i.e. a blue rectangle around a single note), I get:
Debug: cursor Ms::PluginAPI::Cursor(0x1c819b60) segment Ms::PluginAPI::Segment(0x1c7b3eb0) tick 960

But when I select only the note (i.e. I select the note and the notehead is blue), I get:
Debug: cursor Ms::PluginAPI::Cursor(0x1c6c3ea0) segment null tick 0

Perhaps this is related to question 2.

Thanks for your help,


  1. Yes, if you start from the selected elements there is no need to use Cursor to traverse the score

  2. I'd expect them to be ordered by tick, but afaik there is no guaranteed order.

  3. You can .parent on any element until you reach the segment and that is where you'll find the tick

  4. Read "represents" instead of "contains" if that's more comfortable to you. Internally in the code I believe it does "contain" that list, but for the API it doesn't expose the raw list and only the accessors indeed. And yes, only one element can exist in a track for a segment.

  5. cursor.segment will indeed point to the first segment contained within a range selection (keep in mind that the selection API to allow access to list selections if a fairly recent addition)

  6. Yes, cursor.rewind(1) is only supposed to work on a range selection.

In reply to by jeetee

Hi jeetee,

Thank you.

  1. Now that I know how, I could verify this: the selection contains the notes in the order they were selected:
    so if you click on a note, then ctrl-click on a note to the right of it, then ctrl-click on a note to the left of it, and look at the elements in curScore.selection.elements looping using an index, you get the following trick numbers: 840 4320 360.
    That's fine. As long as I know what's happening, I can deal with it.

  2. That's the key (no pun intented :) ) to all the things I tried. I saw the parent property, but didn't figure out that it would lead to a segment.

5-6. The documentation at… says:

enum RewindMode Enumerator
SCORE_START Rewind to the start of a score.
SELECTION_START Rewind to the start of a selection.
SELECTION_END Rewind to the end of a selection.

It would have helped me and saved many houwers, if the explaination was : rewind to the start/end of a range selection.

Instead of using a cursor, and the rewind method, I now loop through the selected notes, keep track of the lowest and highest tick, an use prev/nextInMeasure to find the first segment in the first measure containing a selected note, and the last segment of the last measure containing a selected note, and that was what I was looking for.


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