From 8a14996bb10b3b3a21d2791ad1e72b592196ebe3 Mon Sep 17 00:00:00 2001 From: Christopher Arndt Date: Sat, 20 Jan 2024 10:23:38 +0100 Subject: [PATCH] feat: add first script: 'rename_regions_from_markers.lua' --- rename_regions_from_markers.lua | 69 +++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 rename_regions_from_markers.lua diff --git a/rename_regions_from_markers.lua b/rename_regions_from_markers.lua new file mode 100644 index 0000000..da803e7 --- /dev/null +++ b/rename_regions_from_markers.lua @@ -0,0 +1,69 @@ +ardour { + ["type"] = "EditorAction", + name = "Rename regions from markers", + license = "MIT", + author = "Christopher Arndt", + description = [[Rename selected regions using track name and label of location marker at region start]] +} + +function factory () + -- there is currently no direct way to find the track + -- corresponding to a [selected] region + function find_track_for_region(region_id) + for route in Session:get_tracks():iter() do + local track = route:to_track(); + local pl = track:playlist () + if not pl:region_by_id(region_id):isnil() then + return track + end + end + assert (0) -- can't happen, region must be in a playlist + end + + return function () + local sel = Editor:get_selection () -- get current selection + local loc = Session:locations() -- get locations + + + -- prepare undo operation + Session:begin_reversible_command ("Rename regions from markers") + + -- for each selected region... + for region in sel.regions:regionlist():iter () do + -- test if it's an audio region + local ar = region:to_audioregion () + if ar:isnil () then + goto next + end + + --~ print("Region:", region:name()) + local rid = region:to_stateful():id() + --~ print("ID:", id:to_s()) + + -- get marker at region start position + local pos = region:position() + local mloc = loc:first_mark_at(pos, Temporal.timecnt_t(0)) + if not mloc then + goto next + end + + local track = find_track_for_region(rid) + + region:to_stateful():clear_changes() + + --~ print("Marker label:", mloc:name()) + -- rename region to track name + marker label + region:set_name(track:name() .. " " .. mloc:name()) + + -- collect undo data + Session:add_stateful_diff_command(region:to_statefuldestructible()) + + ::next:: + end + + -- all done, commit the combined Undo Operation + if not Session:abort_empty_reversible_command() then + Session:commit_reversible_command(nil) + end + end +end