Posted on

Finishing your first Reascript

Welcome to the 2nd week of 2017 ladies and gents (at least when I’m writing this post)!  We’re going to start the week off right and do something you didn’t think you could do a few days ago: finish your 2nd Reascript that does some legitimate stuff!

This is going to be a very big update to our script (in relation to what we already have written from all the previous posts.  We’re also going to introduce the concept of if statements and write another loop similar to the one we already have.

Hold onto your pants, I’ll explain it all below.

Map out what we’re adding

Okay first thing’s first – let me describe the additions to our script and we’ll walk through point-by-point what we have to do to add them.

Currently our script will arm the record button on any of the tracks we’ve selected.  But I want it to first check if any tracks are armed that I’ve forgotten about.  If any tracks are armed, I want the script to disarm them, then I’ll re-run the script to get my selected tracks armed.

Keep in mind, you could go about this a completely different way and remove my 2nd step.  That’s fine, and you’re free to code it that way!  But for this example, I’ll be making them two separate steps.

Step-by-Step, what do we need to add?

Okay, to do all of this is going to take a few steps in our code.  We’re going to need the following:

  • The total number of tracks in the project, stored in a variable
  • A true/false variable, also called a boolean, that will store “true” if a track is found that is armed to record
  • A loop to check the record arm status of every track in the project
  • An if statement to check and see if each track is armed, and if it is to turn that off
  • Another if statement that checks our true/false variable is “false”, and therefore arms our tracks if there aren’t any others armed

It seems a little much if you’re brand new, but you’ve already written the hardest part of the code last time.  Trust me!

Let’s make some variables

First, we need to make two new variables to add to our project.  Many programmers create their variables at the beginning of their code (in fact, some languages require this).  I like to do this often for cleanliness purposes if I can.  We will here, because we don’t have a lot of code.  Yours (with comments) should look like the below:

-- Get the total number of tracks in the active project
total_tracks = reaper.CountTracks(0)

-- Get the number of selected tracks in the project
number_of_tracks = reaper.CountSelectedTracks(0)

-- Set a boolean (true/false) variable to false
any_track_armed = false

We still have our old number_of_tracks variable, but now we’ve added two more.  total_tracks uses the API function reaper.CountTracks(0) which gives us the total number of tracks in the active project (that zero again).  any_track_armed is a simple true/false that we’ll use later (set to false now) to find out if any of our tracks is armed to record before we run the script.

Checking if a track is armed

After creating our variables, we want to see if a track is armed.  This requires a loop to go through each of our tracks, and an if statement to check the track’s status.

An if statement is pretty straightforward to explain.  You provide the if statement a condition, and if it’s met then your program executes something.  If your condition isn’t met, it skips over the code within the if statement.

You can think of it as “if this, then do that“.  There are additions to the if statement, the if-else statement and the if-else if statement – but we don’t need those here.

Check out the code below, and I’ll walk through it all with you:

--[[ Loop through all of the tracks in the project
 if we find one with the record arm set,
 set our any_track_armed variable to true to note that
 and un-arm the record on the track]]
for i = 0, total_tracks-1 do
 track = reaper.GetTrack(0,i)
 if reaper.GetMediaTrackInfo_Value(track, "I_RECARM") == 1 then
 any_track_armed = true
 reaper.SetMediaTrackInfo_Value(track, "I_RECARM", 0)
 end
end
  • First we have a for loop, which is written similarly to the one we wrote previously.  This time, we’re looping over all of the tracks in the project instead of just the selected ones.
  • Next we use reaper.GetTrack(0,i) to grab each track one-by-one and store it in a variable each time we go through the loop.  Remember, “i” increases in value by one every time we go through the loop.
  • Third is the if statement.  This looks more complicated than it is.  You’ve previously SetMediaTrackInfo_Value, now you’re just getting instead of setting.  reaper.GetMediaTrackInfo_Value is looking for the record arm status of the track stored in our previous variable.  The function will give us back a 1 if the track is armed to record, and a 0 if it isn’t.  We finish off the if statement with the word “then” – like “if this condition, then do X”
  • From here we jump into our if statement.  If we find a track that’s already armed to record, we want to set any_track_armed equal to true because a track is armed!  After that, we want to disarm that track with reaper.SetMediaTrackInfo_Value again — the reverse of what we did last time.
  • There’s two end statements to finish our code.  You have to tell your program where every loop and if statement ends, and that’s what we’re doing here.  Coincidentally, they end one right after the other.

Arming our selected tracks

Exciting news!  We’ve already written the code for most of this!

Now we’re just going to quickly wrap our previously written code in an if statement, and be done!  Check it out:

--[[ If no tracks are armed to record, loop through the
 selected tracks and arm them all to record ]]
if any_track_armed == false then
 for i = 0, number_of_tracks-1 do
 track = reaper.GetSelectedTrack(0,i)
 reaper.SetMediaTrackInfo_Value(track, "I_RECARM", 1)
 end
end

Hopefully you recognize that for loop in the middle, it’s the code you wrote last time!

What we’re doing here is we’re checking the any_track_armed variable.  If no tracks were armed previously, then we go ahead and arm our selected tracks.  Otherwise, if a track was armed when we ran this script – this part of the code is skipped over and doesn’t execute.

Congratulations!

That’s it!  You should be able to run this script via the actions list in Reaper or a hotkey of your choosing and go about arming your tracks to record quickly.  Or if you’re crafty, you can even use this just to make sure everything is disarmed in a hurry.

Next I’m going to blow your mind a little.  You’re a quick step away from having your third Reascript written already.  I’m going to show you how easy it is to re-purpose your own code, and create something brand new!

The entire script is here for reference also, in case you’re a cheater with copypasta or if you’ve got a bug you can’t figure out:

-- Get the total number of tracks in the active project
total_tracks = reaper.CountTracks(0)

-- Get the number of selected tracks in the project
number_of_tracks = reaper.CountSelectedTracks(0)

-- Set a boolean (true/false) variable to false
any_track_armed = false

--[[ Loop through all of the tracks in the project
 if we find one with the record arm set,
 set our any_track_armed variable to true to note that
 and un-arm the record on the track]]
for i = 0, total_tracks-1 do
 track = reaper.GetTrack(0,i)
 if reaper.GetMediaTrackInfo_Value(track, "I_RECARM") == 1 then
 any_track_armed = true
 reaper.SetMediaTrackInfo_Value(track, "I_RECARM", 0)
 end
end

--[[ If no tracks are armed to record, loop through the
 selected tracks and arm them all to record ]]
if any_track_armed == false then
 for i = 0, number_of_tracks-1 do
 track = reaper.GetSelectedTrack(0,i)
 reaper.SetMediaTrackInfo_Value(track, "I_RECARM", 1)
 end
end

Copyright 2016-2021, NIR LLC, all rights reserved.