Old 05-08-2019, 03:43 PM   #1
silverbeetle_com
Human being with feelings
 
Join Date: Apr 2018
Location: Melbourne, AUS
Posts: 8
Default Select track and insert template

Hi,

I'm currently working on my first Reascript that will accept a track name, find it and insert a track template into it.

I've actually managed to do all of that, I'm just stumbling on a couple points now:

- I successfully selected the parent track but the template inserts into the track that was selected prior to running the script. Any thoughts?

- Probably a totally dumb question, how do I use this reascript in my project when i need to pass one or two parameters to it? My reascript is based on a function called Main that recieves 'trackName' and 'slotNumber'
silverbeetle_com is offline   Reply With Quote
Old 05-08-2019, 03:51 PM   #2
foxAsteria
Human being with feelings
 
foxAsteria's Avatar
 
Join Date: Dec 2009
Location: Oblivion
Posts: 10,271
Default

If you just wanna learn ReaScript, carry on, but I would just make a cycle action for that:

Console command: S [trackname]
insert child track
sws apply template slot x
__________________
foxyyymusic
foxAsteria is online now   Reply With Quote
Old 05-08-2019, 04:31 PM   #3
silverbeetle_com
Human being with feelings
 
Join Date: Apr 2018
Location: Melbourne, AUS
Posts: 8
Default

Yeah, keen to learn Reascript... also there was a bit more to it that I neglected to mention. Like getting a count of the direct children of the parent then looping over the inserted tracks and adding an incremented count value as a suffix to the children's names.

Thanks for the alternate tip!
silverbeetle_com is offline   Reply With Quote
Old 05-09-2019, 04:25 AM   #4
nofish
Human being with feelings
 
nofish's Avatar
 
Join Date: Oct 2007
Location: home is where the heart is
Posts: 12,109
Default

Post the code you have so far?
Makes it easier to help I'd think.
nofish is offline   Reply With Quote
Old 05-09-2019, 04:29 AM   #5
Meo-Ada Mespotine
Human being with feelings
 
Meo-Ada Mespotine's Avatar
 
Join Date: May 2017
Location: Leipzig
Posts: 6,630
Default

Quote:
Originally Posted by silverbeetle_com View Post
Hi,
- Probably a totally dumb question, how do I use this reascript in my project when i need to pass one or two parameters to it? My reascript is based on a function called Main that recieves 'trackName' and 'slotNumber'
Passing parameters to your script is only possible in my Ultraschall-API, but it is an advanced concept and you can't pass them from the GUI of Reaper, only from other scripts into the script that accepts parameters.

There is no native way to pass parameters in Reaper itself, sadly.

What is the purpose of trackName and slotNumber? Maybe there are ways to get that information by your script, other than passing parameters.
__________________
Use you/she/her.Ultraschall-Api Lua Api4Reaper - Donate, if you wish

On vacation for the time being...
Meo-Ada Mespotine is offline   Reply With Quote
Old 05-09-2019, 02:33 PM   #6
silverbeetle_com
Human being with feelings
 
Join Date: Apr 2018
Location: Melbourne, AUS
Posts: 8
Default

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)

Last edited by silverbeetle_com; 05-09-2019 at 02:39 PM. Reason: Had to add extra detail
silverbeetle_com is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 06:05 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.