Hi all - thanks for chiming in!
Yeah - it's quickly becoming obvious to me that trying to pass a value to a reascript function isn't generally done so I've been thinking of work arounds with a combination of cycle actions and breaking apart my original script a bit. I'll continue to mess around with that.
But in the meantime here's the original script incase you want to look. As I mentioned it actually did everything, just not inserting where I selected in the script. If you run it on your machine you'll need to create a folder called 'Lead Group', I also popped an empty track named 'Spacer' in there as part of my template which I hide from view - this forces the otherwise empty Lead Group to actually be a folder. Hacky but I knew no other way and once set up goes unseen
BTW - just noticed that it runs successfully when i save it in the reascript edit screen but not when i try and run it as an action as is... no idea what the difference is there.
My track template is added to resources slot 1, i haven't used a slot param otherwise - was intending to be able to pass in the parent track name and slot number for its track template.
The track template is a folder structure something like this:
"Gtr "
- "SM57 - Gtr "
- "VR1 - Gtr "
which would be renamed like this depending on how many direct children the parent has (excluding the spacer!)
"Gtr 2"
- "SM57 - Gtr 2"
- "VR1 - Gtr 2"
As this is my first reascript please excuse anything stupid, but constructive criticism welcome!
Code:
function FindNumberOfChildrenByName(trackName)
if not trackName then return end
local matched_tr_guids = {}
local lowerCaseTrack = trackName:lower()
local foundMatch = false
local childDepth = 0
local subDepth = 0
local parentIndex
for i = 1, reaper.CountTracks(0) do
track = reaper.GetTrack(0,i-1)
_, tr_name = reaper.GetSetMediaTrackInfo_String( track, 'P_NAME', '', 0 )
iDepth = reaper.GetMediaTrackInfo_Value(track, "I_FOLDERDEPTH")
if (foundMatch == true) then
if (subDepth < 0) then
-- Exit and return number of children
return {childDepth, i-1, parentIndex-1}
else
if (subDepth == 0 and tr_name ~= 'Spacer') then
childDepth = childDepth + 1
end
subDepth = subDepth + iDepth
end
elseif (tr_name:lower():find(lowerCaseTrack)) then
foundMatch = true
parentIndex = i
end
end
-- No match found
return ''
end
function DeselectAllTracks()
reaper.Main_OnCommandEx(40297, 0, 0 )
end
function AddSuffixToSelectedTracks(track, suffix)
local track = reaper.GetTrack(0, 0)
local ok, oldName = reaper.GetSetMediaTrackInfo_String(track, 'P_NAME', '', false)
if ok then
local newName = oldName .. 'suffix'
reaper.GetSetMediaTrackInfo_String(track, 'P_NAME', newName, true)
end
end
function Main(trackName)
local result = FindNumberOfChildrenByName(trackName)
local sel_track
-- Exit if we failed to select the track
if not result then return end
-- Deselect tracks and select the requested parent track
DeselectAllTracks()
sel_track = reaper.GetTrack(0,result[3])
reaper.SetTrackSelected( sel_track, true )
if sel_track then
-- Insert track template
local commandId = reaper.NamedCommandLookup("_S&M_ADD_TRTEMPLATE1")
reaper.Main_OnCommand(commandId, 0)
-- Loop over selected tracks and add an incremented counter
countTracks = reaper.CountSelectedTracks(0)
if countTracks > 0 then
for j = 0, countTracks-1 do
local track = reaper.GetSelectedTrack(0, j)
local ok, oldName = reaper.GetSetMediaTrackInfo_String(track, 'P_NAME', '', false)
if ok then
local newName = oldName .. result[1]+1
reaper.GetSetMediaTrackInfo_String(track, 'P_NAME', newName, true)
end
end
end
end
end
result = Main("Lead Group", 1)