ardour-scripts/rename_regions_from_markers...

72 lines
2.3 KiB
Lua

ardour {
["type"] = "EditorAction",
name = "Rename regions from markers",
license = "MIT",
author = "Christopher Arndt",
description = [[Rename selected regions using track name and label of range marker at region start]]
}
function factory()
-- there is currently no direct way to find the track
-- corresponding to a [selected] region
---@param region_id ID
---@return Track?
local 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
if region:to_audioregion():isnil() then
goto next
end
--~ print("Region:", region:name())
local rid = region:to_stateful():id()
--~ print("ID:", rid:to_s())
-- get range marker at region start position
local pos = region:position()
local mloc = loc:range_starts_at(pos, Temporal.timecnt_t(0), true)
if not mloc then
goto next
end
local track = find_track_for_region(rid)
if track then
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())
end
::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