Clefside JavaScript Automation API
Read, edit, play, and export scores through direct JavaScript calls in the editor tab. Open the editor, wait for window.clefside, then use the methods on that object. In Playwright, call them with page.evaluate(). The editor updates and saves changes in its normal browser storage.
Read the JSON contract for arguments, edit commands, native score structure, and instrument and notation catalogs. The same information is available from window.clefside.describe().
Create and export a score with Playwright
await page.goto("https://clefside.com/");
await page.waitForFunction(() => window.clefside?.version === 1);
const file = await page.evaluate(async () => {
const clefside = window.clefside;
const { revision } = clefside.newScore({ title: "First melody" });
clefside.applyEdits({ revision, edits: [
{ type: "addNote", measure: 1, beat: 1, pitch: "C4" },
{ type: "addNote", measure: 1, beat: 2, pitch: "D4" },
{ type: "addNote", measure: 1, beat: 3, pitch: "E4" },
{ type: "addRest", measure: 1, beat: 4 }
] });
return await clefside.exportScore({ format: "musicxml" });
});
// file contains filename, mimeType, encoding, and data.
Working with scores
newScore(),openExample(), andimportScore()open independent local drafts by default. Usetarget: "current"to replace the open score as an undoable edit.openDraft()opens an existing saved score, with a copy if another tab is editing it.- Use
getScore()to find staff, instrument, measure, and event IDs. Measures, beats, and voices start at one. The native score format uses zero-based voices and tick offsets, with 1,920 ticks per quarter note. - Each successful edit batch is one undo step. An invalid batch throws an error and leaves the open score unchanged. Supply the
revisionfromgetState()to detect changes since your last read. UsevalidateScore()andreplaceScore()when editing an entire native score object. - Text imports take the file contents. Pass MXL and MIDI as base64. Text exports return a string and MIDI exports return base64. Use
download: trueif you want the browser to download an export as well. - Use
getFingering({ eventId })to find the strings that work for each pitch in a chord. Apply asetFingeringedit with the event ID, zero-based pitch index and string number. Passnullfor the string to restore automatic fingering. - Use
getScoreLayout()to read measure ranges, page boundaries and staff or tablature rows. To show help inside the editor, callopenPanel({ name: "tutorial", chapter: "write" }).closePanel()dismisses the active panel. - Check
getState().audioafter requesting playback. A browser may require user interaction before audio starts or beforeopenScorePicker()opens the native file chooser. For converting PDFs into editable notation, follow the MusicXML guide.
Save an A4 PDF
Use printScore() to open the browser’s Print dialog. For a PDF returned as bytes, use the Chromium print engine after Clefside renders all pages.
await page.evaluate(() => window.clefside.preparePrint());
try {
const pdf = await page.pdf({ preferCSSPageSize: true, printBackground: true });
// Save or return the PDF bytes here.
} finally {
await page.evaluate(() => window.clefside.finishPrint());
}
Methods
describe- Get the API version, documentation URL, callable methods, JSON Schemas and music catalogs.
getPlaybackTimeline- Read timing, sound events, pitches and dynamics for the open score.
getScore- Read a detached native score as JSON. Use its IDs for targeted edits.
getState- Read the draft ID, score revision, selection, view, active panel, input settings, history and playback status.
getScoreLayout { layout? }- Read the displayed layout or request page or continuous layout. Returns one-based measure ranges and staff/tab rows.
getFingering { eventId }- Read an event’s tuning, resolved pitches, current frets and playable string choices. Pitch indices start at zero.
validateScore { version, title, composer, key, meter, tempo, parts, measures, hairpins? }- Validate and normalize a native score without changing the editor.
replaceScore { score, revision? }- Replace the open score as one undoable edit using the full native score model.
applyEdits { edits, revision? }- Apply a batch atomically. Returns created IDs and any normalization warnings.
newScore { target?, title? }- Open a blank, independent local draft by default.
openExample { name, target? }- Open a built-in example in an independent local draft by default.
listDrafts- List saved drafts and recovery entries in this browser.
openDraft { id }- Open a saved draft by ID. Use an empty string for the main score. An occupied draft opens as a copy.
recoverDraft { recoveryKey, download? }- Read the original text of a recovery entry using its recoveryKey from listDrafts. Optionally download it.
openScorePicker { format? }- Open the editor’s native file chooser. Requires browser user activation. Use importScore for data supplied by code.
importScore { name, data, encoding?, target? }- Import native Clefside, MusicXML, MXL or MIDI. Pass file contents as text or as base64 for binary files.
exportScore { format, download? }- Return a Clefside, MusicXML or MIDI file. MIDI is returned as base64. Optionally start a browser download.
select { eventId?, pitchIndex?, preview?, measure?, staffId?, at?, beat? }- Select an event by ID or move the cursor to a staff, measure and beat. Preview can sound a selected note.
setInput { tool?, voice?, duration?, value?, dots?, tuplet?, accidental?, fret?, drum? }- Set the on-screen editor input tool, rhythm, accidental, fret or drum piece.
setView { layout?, zoom?, textScore?, mobileTools?, noteNames?, noteLabels? }- Set page or continuous layout, zoom, text score, note names, note labels or mobile tools.
openPanel { name, chapter?, partId? }- Open an editor dialog or feedback. For instrument settings, partId defaults to the selected instrument.
closePanel- Close the active editor dialog or feedback panel.
dismissNotice- Dismiss the editor’s current status or error message.
setAudio { quality?, pitchPreview? }- Set SoundFont quality or note preview.
playback { action, measure?, staffId?, at?, beat? }- Play from the cursor or a score position, pause, resume or stop. Browser audio policies apply.
printScore- Open the browser’s A4 print / save as PDF dialog for the current score.
preparePrint- Render all score pages using the same A4 layout as Print. Returns the page count. Use the browser PDF engine, then call finishPrint.
finishPrint- Restore the normal editor view after preparePrint.
history { direction }- Undo or redo an edit.
getResources- Get links for guides, examples, privacy, audio credits and feedback email.
Edit commands
setScore { title?, composer?, key?, meter?, tempo? }- Set title, composer, or the default key, meter and tempo.
insertMeasure { after?, count? }- Insert after a measure, append by default, or use after: 0 to insert first.
removeMeasure { measure }- Remove a bar and keep inherited settings on the following bar.
setMeasure { measure, meter?, key?, tempo?, end? }- Set per-measure key, meter, tempo or barline. Null restores inheritance or clears the barline.
addInstrument { instrument }- Add an instrument with its usual staves.
setInstrument { partId, name?, program?, volume?, pan?, notation? }- Set the name, General MIDI program, volume, pan or tablature view.
removeInstrument { partId }- Remove an instrument and its events. A score needs at least one staff.
addStaff { partId, clef }- Add a staff to an instrument.
removeStaff { staffId }- Remove a staff and its notes and symbols.
setClef { staffId, measure?, clef }- Set a staff clef. Omit measure for the staff default. Null on a measure restores inheritance.
addNote { measure?, staffId?, at?, beat?, pitch?, pitches?, drum?, tab?, voice?, duration?, value?, dots?, tuplet?, dynamic?, articulation?, trill?, playback?, tie?, slur? }- Insert a note, chord, drum hit, or fretted tab note. Use one pitch input. Default duration is quarter.
addRest { measure?, staffId?, at?, beat?, voice?, duration?, value?, dots?, tuplet? }- Insert a rest. Default duration is quarter.
setEvent { eventId, measure?, staffId?, at?, beat?, pitch?, pitches?, drum?, tab?, voice?, duration?, value?, dots?, tuplet?, dynamic?, articulation?, trill?, playback?, tie?, slur?, rest? }- Move or change a note or rest. Use rest: true to clear pitches and null to clear a marking.
setFingering { eventId, pitchIndex, string }- Assign a tablature string to a chord tone, or pass null to restore automatic fingering.
removeEvent { eventId }- Delete a note, chord, drum hit or rest.
setChordSymbol { measure?, staffId?, at?, beat?, symbol }- Set or remove a chord symbol at a score position. Null removes it.
addHairpin { staffId, kind, start, end }- Add a crescendo or diminuendo between two positions on the same staff.
removeHairpin { index }- Remove a hairpin by its zero-based index in getScore().hairpins.