| 1 | local output_path = OUTPUT_PATH or os.getenv("REAPER_ACTIONS_OUTPUT_PATH") |
| 2 | if not output_path or output_path == "" then |
| 3 | error("REAPER_ACTIONS_OUTPUT_PATH is not set") |
| 4 | end |
| 5 | |
| 6 | local section = reaper.SectionFromUniqueID(0) |
| 7 | if not section then |
| 8 | error("Failed to resolve the main action section") |
| 9 | end |
| 10 | |
| 11 | local function json_escape(value) |
| 12 | return value:gsub('[%z\1-\31\\"]', function(char) |
| 13 | if char == "\\" then |
| 14 | return "\\\\" |
| 15 | end |
| 16 | if char == "\"" then |
| 17 | return "\\\"" |
| 18 | end |
| 19 | if char == "\b" then |
| 20 | return "\\b" |
| 21 | end |
| 22 | if char == "\f" then |
| 23 | return "\\f" |
| 24 | end |
| 25 | if char == "\n" then |
| 26 | return "\\n" |
| 27 | end |
| 28 | if char == "\r" then |
| 29 | return "\\r" |
| 30 | end |
| 31 | if char == "\t" then |
| 32 | return "\\t" |
| 33 | end |
| 34 | |
| 35 | return string.format("\\u%04x", char:byte()) |
| 36 | end) |
| 37 | end |
| 38 | |
| 39 | local handle = assert(io.open(output_path, "wb")) |
| 40 | handle:write("[\n") |
| 41 | |
| 42 | local first = true |
| 43 | local index = 0 |
| 44 | |
| 45 | while true do |
| 46 | local command_id, name = reaper.kbd_enumerateActions(section, index) |
| 47 | if command_id == 0 then |
| 48 | break |
| 49 | end |
| 50 | |
| 51 | if name and name ~= "" then |
| 52 | if not first then |
| 53 | handle:write(",\n") |
| 54 | end |
| 55 | first = false |
| 56 | handle:write( |
| 57 | string.format( |
| 58 | ' {"commandId":%d,"name":"%s"}', |
| 59 | command_id, |
| 60 | json_escape(name) |
| 61 | ) |
| 62 | ) |
| 63 | end |
| 64 | |
| 65 | index = index + 1 |
| 66 | end |
| 67 | |
| 68 | handle:write("\n]\n") |
| 69 | handle:close() |