Getting Started with CakeScript in Cakewalk Sonar

CakeScript extends Lua with Sonar project objects and commands. It replaces many uses of the legacy Cakewalk Application Language (CAL) with a modern scripting environment.

CakeScript can open and save projects; create and edit tracks, buses, folders, clips, sends, plugins, ProChannel modules, automation, tempo, meter and key changes, markers, Arranger data, lyrics, selections, settings, and other Sonar data; run Sonar commands; and call other scripts.

Typical uses include:

Apply a repeatable setup

Create a set of buses, name them, and route selected tracks.

Batch-edit a project

Rename tracks, change colors, or set a property on every matching track.

Transform MIDI

Inspect or replace events, transpose notes, or split events by channel.

Create a report

Print track, routing, plugin, marker, tempo, or project information.

Build a custom workflow

Display a form, collect user choices, and apply the result as one undoable operation.

CakeScript is intended for users who are comfortable reading or writing Lua. The CakeScript API is not compatible with another application's Lua API.

CakeScript View in Cakewalk Sonar

Location

In the main menu bar:

  • Select Process > Run CakeScript... to choose and run a .lua file directly.
  • Select Views > CakeScript View to browse, run, trace, and step through scripts.
  • Select Help > CakeScript Reference Guide for the complete API reference.
  • Set the script root folder in Preferences > File > Folder Locations > CakeScript Files.

CakeScript files use the .lua extension.

Quick start: Run an existing script

  1. Stop playback and recording.
  2. Select Process > Run CakeScript....
  3. Choose a .lua file.
  4. Complete any form displayed by the script.
  5. Review the Output view or completion message.
  6. Verify the project changes. Use Undo to revert the script as one operation if needed.

CakeScript View

Scripts tab

The Scripts tab displays .lua files and folders under the configured CakeScript Files folder.

  • Single-click a file to select it.
  • Select the Run button to execute the selected script.
  • Double-click a file to select and run it.
  • The list refreshes automatically when files are added or removed.

Output tab

The Output tab displays script progress, print() output, trace information, and errors. The view switches to Output when a script starts.

Run modes

Normal

Runs the script normally.

Trace

Runs while reporting execution details in Output.

Step

Pauses at script steps. Use the Step button to continue one step at a time.

Use Stop to cancel a running script when cancellation is available.

Script behavior and undo

  • CakeScript cannot start while the transport is running.
  • Successful project edits are grouped into one item in Sonar's Undo history.
  • If a script fails, Sonar rolls back its project edits and reports the error.
  • Nested scripts participate in the owning run's transaction.
  • A script cannot overlap another script run or a pending script result dialog.

Important

External file changes, programs started by a script, and other effects outside the Sonar project are not reversed by project Undo.

CakeScript object model

The global cakeapp object is the entry point. Its main hierarchy is:

  • cakeapp
    • projects
      • project
        • tracks
          • track
            • clips
            • plugins
            • sends
            • automations
        • selection
        • tempos
        • markers
        • meterKeys
        • lyricsTrack
        • arrangerTracks
        • arrangements
        • settings
        • cmds

Buses and folders are represented as Track objects. Collections use 1-based indexing.

Lua and CakeScript conventions

  • Properties start with a lower-case letter and use dot notation: track.name.
  • Methods use UpperCamelCase and colon notation: tracks:GetAt(1).
  • Collection indexes are 1-based: GetAt(1) returns the first item.
  • IDs such as trackId and clipId are opaque strings. Do not construct or modify them.
  • Objects can become stale after deletion or structural changes. Check isValid or retrieve the object again before reusing it.
  • Use the API's documented time and unit conversions instead of assuming raw values.

Example: List the tracks in the active project

local project = cakeapp.projects:GetActiveProject()
local tracks = project.tracks

for trackIndex = 1, tracks.count do
    local track = tracks:GetAt(trackIndex)
    print(trackIndex .. ": " .. track.name)
end

Output and completion messages

  • print(...) writes output that is shown when the script completes and in CakeScript View Output.
  • Assign a string to the global cakeout variable to display a completion toast.
print("Processed selected tracks")
cakeout = "The CakeScript operation completed successfully."

User input

Scripts can display standard input prompts or build a UIDialog containing labels, text fields, multiline fields, choices, toggle switches, time values, file and folder selectors, and buttons. A dialog can validate mandatory fields before allowing submission.

Use a dialog when the same script needs user-selectable tracks, values, files, or operation modes. Keep destructive options explicit and summarize the operation before applying it.

Writing safer scripts

  • Save important projects before testing a new script.
  • Start with a copy of the project when testing destructive or broad edits.
  • Validate that the active project and target objects exist.
  • Prefer stable IDs over collection positions when a script changes project structure.
  • Check isValid after deleting, moving, or replacing objects.
  • Report what the script changed with print() or cakeout.
  • Treat scripts from other people as executable code. Read them before running them.
  • Do not place passwords, API keys, or other secrets in scripts or output.

Troubleshooting & gotchas

Stale object references

Track or clip objects retrieved before structural edits or deletions become invalid. Always re-query objects or verify isValid before calling methods.

Execution during playback

CakeScript execution is blocked while the audio engine transport is active. Stop playback before running scripts.

External side effects

While internal Sonar project changes roll back cleanly on errors, file modifications or external commands initiated by Lua cannot be reversed by project Undo.

CakeScript and AI Assist

AI Assist can use native Sonar tools for many operations and can propose CakeScript for more complex workflows. Before allowing a generated script:

  1. Read the complete script shown in the confirmation card.
  2. Check file paths, deletion operations, plugin changes, and loops over project objects.
  3. Confirm that the script targets the intended project and selection.
  4. Approve it only when the requested result and side effects are clear.

Generated scripts follow the same transport, transaction, error, and Undo rules as scripts launched directly in Sonar.

0 out of 0 found this helpful