Last updated:

ReaScript Video Tutorial — Part. 7: Loop in Items on Selected Tracks

In this video, we learn

  • How to count the selected track number
  • How to loop on selected tracks
  • How to loop on items on tracks

From a code perspective, you will learn

  • How to make a nested loop
  • How to break a loop

Let’s go !

Code Snippet:

function Msg(param)
  reaper.ShowConsoleMsg(tostring(param).."\n")
end

function Main()
    
    reaper.Main_OnCommand(40289, 0)
    
    item_mouse, mouse_pos  = reaper.BR_ItemAtMouseCursor()
    
    if tostring(item_mouse) ~= "userdata: 0000000000000000" then
    
        item_mouse_fadeout = reaper.GetMediaItemInfo_Value(item_mouse, "D_FADEOUTLEN")
        
        start_time, end_time = reaper.GetSet_LoopTimeRange2(0, false, false, 0, 0, false)
        
        if start_time ~= end_time then
            
            Action()
        
        end
    
    end
    
end

function Action()
    
    count_sel_tracks = reaper.CountSelectedTracks(0)
    
    for j = 0, count_sel_tracks - 1 do
    
        track = reaper.GetSelectedTrack(0, j)
        
        count_items_on_track = reaper.CountTrackMediaItems(track)
        
        for i = 0, count_items_on_track - 1 do
            
            item = reaper.GetTrackMediaItem(track, i)
            
            item_pos = reaper.GetMediaItemInfo_Value(item, "D_POSITION")
            
            if item_pos > end_time then
            
                break
            
            else
            
                item_len = reaper.GetMediaItemInfo_Value(item, "D_LENGTH")
                item_end = item_pos + item_len
                
                if (item_pos > start_time and item_pos < end_time) or (item_end > start_time and item_end < end_time) or (item_pos < start_time and item_end > end_time) then
                    
                        
                    reaper.SetMediaItemInfo_Value(item, "D_FADEOUTLEN", item_mouse_fadeout)
                
                end
                
            end
            
        end
    
    end
    
    reaper.UpdateArrange()

end

reaper.ShowConsoleMsg("")
Main()

Next videos will show you how to use Native Actions right in your script !

Help us translate the subtitles of this video in your language using Amara.org !