Old 01-29-2019, 07:52 PM   #1
lexaproductions
Human being with feelings
 
Join Date: Jan 2013
Posts: 1,126
Default Selecting a track by name

I've read a few threads but it's still not clear for me.
Someone can tell me how to simply select a track named "Structure" in lua?
In my ignorance, I've tried
Code:
reaper.SetOnlyTrackSelected("Structure")
But it obviously didn't work.

Please somebody help a newbie?

Thanks
lexaproductions is offline   Reply With Quote
Old 01-29-2019, 08:12 PM   #2
cfillion
Human being with feelings
 
cfillion's Avatar
 
Join Date: May 2015
Location: Québec, Canada
Posts: 4,937
Default

Iterate through all tracks until you find the one with the correct name, then select it.

Code:
function getTrackByName(name)
  for trackIndex = 0, reaper.CountTracks(0) - 1 do
    local track = reaper.GetTrack(0, trackIndex)
    local ok, trackName = reaper.GetSetMediaTrackInfo_String(track, 'P_NAME', '', false)

    if ok and trackName == name then
      return track -- found it! stopping the search here
    end
  end
end

local track = getTrackByName("Structure")

if track then -- if a track named "Structure" was found
  reaper.SetOnlyTrackSelected(track)
end
I assume this is for a bigger script, but if it's only to be used as an action, it can be done using the 'S' ReaConsole command (SWS). ReaConcole commands can be put in custom cycle actions.


Last edited by cfillion; 01-29-2019 at 08:18 PM.
cfillion is offline   Reply With Quote
Old 01-29-2019, 08:32 PM   #3
lexaproductions
Human being with feelings
 
Join Date: Jan 2013
Posts: 1,126
Default

I would LOVE to use the reaconsole but I understood that it is not possible to use it in a reascript?
lexaproductions is offline   Reply With Quote
Old 01-29-2019, 08:39 PM   #4
cfillion
Human being with feelings
 
cfillion's Avatar
 
Join Date: May 2015
Location: Québec, Canada
Posts: 4,937
Default

ReaConsole commands can be used from a script by running a cycle action containing the command (I wouldn't recommend it though). If your goal is to integrate track selection into a longer script, it's much better to do it directly using the scripting API (as shown above).
cfillion is offline   Reply With Quote
Old 01-29-2019, 09:01 PM   #5
lexaproductions
Human being with feelings
 
Join Date: Jan 2013
Posts: 1,126
Default

1- Why don't you recommand it?
2- Isn't it a bit overkill to scroll through ALL the tracks to pick only one? this won't be long to do?
lexaproductions is offline   Reply With Quote
Old 01-29-2019, 11:17 PM   #6
mpl
Human being with feelings
 
mpl's Avatar
 
Join Date: Oct 2013
Location: Moscow, Russia
Posts: 3,960
Default

Quote:
Originally Posted by lexaproductions View Post
2- Isn't it a bit overkill to scroll through ALL the tracks to pick only one? this won't be long to do?
This is how console works also: https://github.com/reaper-oss/sws/bl...nsole.cpp#L281
mpl is offline   Reply With Quote
Old 01-29-2019, 11:24 PM   #7
lexaproductions
Human being with feelings
 
Join Date: Jan 2013
Posts: 1,126
Default

Quote:
Originally Posted by mpl View Post
Thanks! It's comforting to know...
lexaproductions is offline   Reply With Quote
Old 01-30-2019, 01:22 AM   #8
lexaproductions
Human being with feelings
 
Join Date: Jan 2013
Posts: 1,126
Default

One last thing. How can I make a limit to protect if there is no "Structure" track in the project?
lexaproductions is offline   Reply With Quote
Old 01-30-2019, 07:12 AM   #9
cfillion
Human being with feelings
 
cfillion's Avatar
 
Join Date: May 2015
Location: Québec, Canada
Posts: 4,937
Default

Quote:
Originally Posted by lexaproductions View Post
1- Why don't you recommand it?
Because it's troublesome to setup (less portable), harder to debug and makes the script depend on the cycle action always having the same specific command ID. There's pretty much no upsides...

Quote:
Originally Posted by lexaproductions View Post
2- Isn't it a bit overkill to scroll through ALL the tracks to pick only one? this won't be long to do?
Checking the name of 1,000 tracks in Lua takes around half a millisecond here. Running a ReaConsole cycle action that does the same takes ~8 ms.

Quote:
Originally Posted by lexaproductions View Post
One last thing. How can I make a limit to protect if there is no "Structure" track in the project?
The getTrackByName function from my example above returns nil if no matching tracks are found. "if track then reaper.SetOnlyTrackSelected(track) end" checks for this and only attempts to select a track if one was found.

Last edited by cfillion; 01-30-2019 at 07:28 AM.
cfillion is offline   Reply With Quote
Old 07-07-2020, 12:31 PM   #10
Buy One
Human being with feelings
 
Buy One's Avatar
 
Join Date: Sep 2019
Posts: 1,134
Default

And how do you condition an error message for when a track with a desired name wasn't found?

I tried to use if trackName ~= name then but this condition is true even when the track with the desired name is there, because it matches the rest of the tracks with different names

Last edited by Buy One; 07-07-2020 at 12:39 PM.
Buy One is offline   Reply With Quote
Old 07-07-2020, 02:42 PM   #11
jkooks
Human being with feelings
 
Join Date: May 2020
Posts: 190
Default

You can just put an else statement after the "if track then":

Code:
if track then -- if a track named "Structure" was found/that track doesn't equal nil
  reaper.SetOnlyTrackSelected(track)
else -- track == nil/no track with that name was
  reaper.ShowMessageBox("There is no track with that name." "Error: Name Not Found", 0)
end

The statement is essentially saying if track isn't nil (a track with that name was found) then select it, otherwise if the track is nil (no track with that name is found) then show the error message.
jkooks is offline   Reply With Quote
Old 07-08-2020, 10:37 AM   #12
Buy One
Human being with feelings
 
Buy One's Avatar
 
Join Date: Sep 2019
Posts: 1,134
Default

Thank you, jkooks, in my code i couldn't make it work so i ended up following reaper.SetOnlyTrackSelected(track) with reaper.GetSelectedTrack(0,0) to check if track exists
Buy One 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 02:35 AM.


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