1local output_path = OUTPUT_PATH or os.getenv("REAPER_ACTIONS_OUTPUT_PATH")
2if not output_path or output_path == "" then
3 error("REAPER_ACTIONS_OUTPUT_PATH is not set")
4end
5
6local section = reaper.SectionFromUniqueID(0)
7if not section then
8 error("Failed to resolve the main action section")
9end
10
11local 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)
37end
38
39local handle = assert(io.open(output_path, "wb"))
40handle:write("[\n")
41
42local first = true
43local index = 0
44
45while 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
66end
67
68handle:write("\n]\n")
69handle:close()