Old Yesterday, 12:03 PM   #1
jguitarguy27
Human being with feelings
 
Join Date: Oct 2007
Posts: 100
Default Can someone help me with the loop for this script?

Hi all,

I'm new to Reascript and lua, and I'm hoping someone can help me connect the dots for what I have so far.

So I'm trying to make a script that looks at the selected items, and moves them to the next track if they're stereo. I made this little script that works for a single selected item:

=================================================
MediaItem = reaper.GetSelectedMediaItem( 0, 0 )
MediaItem_Take = reaper.GetActiveTake( MediaItem )
pcm_source = reaper.GetMediaItemTake_Source( MediaItem_Take )
ch = reaper.GetMediaSourceNumChannels( pcm_source )
MediaTrack = reaper.GetMediaItemTrack( MediaItem )

if ch == 2 then
reaper.Main_OnCommand(40118, 0)
end
=================================================

Now, I know that in order to make this work on a group of selected items, it needs to be encapsulated in some kind of "for" loop (I think). Could someone help me with the syntax of this?
jguitarguy27 is offline   Reply With Quote
Old Yesterday, 12:24 PM   #2
Ben Osterhouse
Human being with feelings
 
Join Date: Jan 2019
Posts: 45
Default

Here is one way you could do it:

Code:
num_selected_items = reaper.CountSelectedMediaItems(0)

selected_items = {}
for i = 0, num_selected_items - 1 do
	selected_items[i] = reaper.GetSelectedMediaItem( 0, i )
end
for i = 0, num_selected_items - 1 do
	reaper.SetMediaItemSelected(selected_items[i], false)
end

for i = 0, num_selected_items - 1 do
	MediaItem = selected_items[i]
	MediaItem_Take = reaper.GetActiveTake( MediaItem )
	pcm_source = reaper.GetMediaItemTake_Source( MediaItem_Take )
	ch = reaper.GetMediaSourceNumChannels( pcm_source )
	MediaTrack = reaper.GetMediaItemTrack( MediaItem )

	if ch == 2 then
		reaper.SetMediaItemSelected(MediaItem, true)
	end
end
reaper.Main_OnCommand(40118, 0)
reaper.UpdateArrange()
Ben Osterhouse is offline   Reply With Quote
Old Yesterday, 12:33 PM   #3
jguitarguy27
Human being with feelings
 
Join Date: Oct 2007
Posts: 100
Default

Wow that's great thank you. I'm going to study this and learn from it!
jguitarguy27 is offline   Reply With Quote
Old Yesterday, 12:45 PM   #4
Ben Osterhouse
Human being with feelings
 
Join Date: Jan 2019
Posts: 45
Default

No problem, yeah I was trying to do something similar to this recently and got really tripped up because I didn't realize you have to reaper.UpdateArrange(). Otherwise when you've selected or unselected items, the new selections won't actually show up until you click somewhere.
Ben Osterhouse is offline   Reply With Quote
Old Yesterday, 02:30 PM   #5
lexaproductions
Human being with feelings
 
Join Date: Jan 2013
Posts: 589
Default

If you want to do your search only on the currently selected items, you might wanna try to do it the other way around:

Loop through the selected items and for each item:
Look if it's stereo
If not then deselect it.
When you're done. move the remainder to the next track.
__________________
My Patreon address: Reaper4Live
lexaproductions is offline   Reply With Quote
Old Yesterday, 02:35 PM   #6
Ben Osterhouse
Human being with feelings
 
Join Date: Jan 2019
Posts: 45
Default

O yeah that would be simpler, I didn't think of that.
Ben Osterhouse is offline   Reply With Quote
Old Today, 12:16 PM   #7
Triode
Human being with feelings
 
Triode's Avatar
 
Join Date: Jan 2012
Posts: 733
Default

This is a good showcase of how to use an array for selected items and do stuff to them one at a time. This has always been a mystery to me!
__________________
Mixing / Brush and Beater Drums Online: www.outoftheboxsounds.com
Triode is offline   Reply With Quote
Old Today, 01:14 PM   #8
Edgemeal
Human being with feelings
 
Edgemeal's Avatar
 
Join Date: Apr 2016
Location: ASU`ogacihC
Posts: 2,067
Default

You wouldn't actually need an array for this, if the code is changing the selected state of the selected items then all you need to do is loop backwards, basically what lexaproductions said...

Code:
for i = reaper.CountSelectedMediaItems(0)-1, 0, -1 do -- loop backwards
  MediaItem = reaper.GetSelectedMediaItem( 0, i )
  MediaItem_Take = reaper.GetActiveTake( MediaItem )
  pcm_source = reaper.GetMediaItemTake_Source( MediaItem_Take )
  ch = reaper.GetMediaSourceNumChannels( pcm_source )
  if ch ~= 2 then reaper.SetMediaItemSelected(MediaItem, false) end
end
reaper.Main_OnCommand(40118, 0)
reaper.UpdateArrange()
Edgemeal is online now   Reply With Quote
Old Today, 01:24 PM   #9
Ben Osterhouse
Human being with feelings
 
Join Date: Jan 2019
Posts: 45
Default

Hey there we go! Yeah because looping forward, the index would get messed up.
Ben Osterhouse 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 10:55 PM.


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