reaper.Undo_BeginBlock()
reaper.PreventUIRefresh(-1)
for i = 0, reaper.CountMediaItems(0) - 1 do
reaper.SetMediaItemSelected(reaper.GetMediaItem(0, i), false)
end
reaper.BR_GetMouseCursorContext()
local item = reaper.BR_GetMouseCursorContext_Item()
reaper.SetMediaItemSelected(item, true)
local tr = reaper.GetMediaItemTrack(item)
if reaper.GetMediaTrackInfo_Value(tr, 'I_FOLDERDEPTH') ~= 1 then return end
reaper.Main_OnCommand(reaper.NamedCommandLookup'_SWS_SAVESEL',0) -- SWS: Save current track selection
local t_pos, t_end = {}, {}
reaper.SetOnlyTrackSelected(tr)
reaper.Main_OnCommand(reaper.NamedCommandLookup'_SWS_SELCHILDREN',0) -- Select only children of selected folders
reaper.DeleteTrackMediaItem(tr, reaper.GetTrackMediaItem (tr, 0))
for s = 0, reaper.CountSelectedTracks(0) - 1 do
local child = reaper.GetSelectedTrack(0, s)
for i = 0, reaper.CountTrackMediaItems(child) - 1 do
local ch_item = reaper.GetTrackMediaItem(child, i)
reaper.SetMediaItemSelected(ch_item, true)
if ch_item then
local pos = reaper.GetMediaItemInfo_Value(ch_item, 'D_POSITION')
table.insert(t_pos, pos)
local lng = reaper.GetMediaItemInfo_Value(ch_item, 'D_LENGTH')
local fin = pos + lng
table.insert(t_end, fin)
end
end
end
table.sort(t_pos)
table.sort(t_end)
local starttime = t_pos[1]
local endtime = t_end[#t_end]
if starttime and endtime then
reaper.CreateNewMIDIItemInProj(tr, starttime, endtime, 0)
local item = reaper.GetTrackMediaItem(tr, 0)
local retval, tr_name = reaper.GetTrackName(tr, '')
local take = reaper.GetMediaItemTake(item, 0)
reaper.GetSetMediaItemTakeInfo_String(take, 'P_NAME', tr_name, 1)
reaper.SetMediaItemInfo_Value(item, 'I_CUSTOMCOLOR', 20788777.0)
reaper.SetMediaItemSelected(item, true)
end
reaper.Main_OnCommand(reaper.NamedCommandLookup'_SWS_RESTORESEL',0) -- SWS: Restore saved track selection
reaper.PreventUIRefresh(1)
reaper.Undo_EndBlock("Change media item selection", -1)