|
|
|
01-29-2019, 07:52 PM
|
#1
|
Human being with feelings
Join Date: Jan 2013
Posts: 1,126
|
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
|
|
|
01-29-2019, 08:12 PM
|
#2
|
Human being with feelings
Join Date: May 2015
Location: Québec, Canada
Posts: 4,937
|
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.
|
|
|
01-29-2019, 08:32 PM
|
#3
|
Human being with feelings
Join Date: Jan 2013
Posts: 1,126
|
I would LOVE to use the reaconsole but I understood that it is not possible to use it in a reascript?
|
|
|
01-29-2019, 08:39 PM
|
#4
|
Human being with feelings
Join Date: May 2015
Location: Québec, Canada
Posts: 4,937
|
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).
|
|
|
01-29-2019, 09:01 PM
|
#5
|
Human being with feelings
Join Date: Jan 2013
Posts: 1,126
|
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?
|
|
|
01-29-2019, 11:17 PM
|
#6
|
Human being with feelings
Join Date: Oct 2013
Location: Moscow, Russia
Posts: 3,960
|
Quote:
Originally Posted by lexaproductions
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
|
|
|
01-29-2019, 11:24 PM
|
#7
|
Human being with feelings
Join Date: Jan 2013
Posts: 1,126
|
Quote:
Originally Posted by mpl
|
Thanks! It's comforting to know...
|
|
|
01-30-2019, 01:22 AM
|
#8
|
Human being with feelings
Join Date: Jan 2013
Posts: 1,126
|
One last thing. How can I make a limit to protect if there is no "Structure" track in the project?
|
|
|
01-30-2019, 07:12 AM
|
#9
|
Human being with feelings
Join Date: May 2015
Location: Québec, Canada
Posts: 4,937
|
Quote:
Originally Posted by lexaproductions
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
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
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.
|
|
|
07-07-2020, 12:31 PM
|
#10
|
Human being with feelings
Join Date: Sep 2019
Posts: 1,134
|
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.
|
|
|
07-07-2020, 02:42 PM
|
#11
|
Human being with feelings
Join Date: May 2020
Posts: 190
|
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.
|
|
|
07-08-2020, 10:37 AM
|
#12
|
Human being with feelings
Join Date: Sep 2019
Posts: 1,134
|
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
|
|
|
Thread Tools |
|
Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -7. The time now is 02:35 AM.
|