-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMoveSelectedItemsToChildTracks.lua
47 lines (36 loc) · 1.46 KB
/
MoveSelectedItemsToChildTracks.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
local _, path = reaper.get_action_context()
local folder_path = path:match('^.+[\\/]')
package.path = folder_path .. '?.lua;'
local btk = require 'btk'
local rpr = require 'rpr'
btk.main("MoveSelectedItemsToChildTracks", function()
local sourceItems = btk.GetSelectedItems()
if #sourceItems == 0 then
return
end
local savedCursorPosition = reaper.GetCursorPosition()
local savedSelectedTracks = btk.GetSelectedTracks()
local firstItem = sourceItems[1]
local firstItemInfo = btk.GetItemInfo(firstItem)
local sourceTrack = firstItemInfo.track
local sourceTrackInfo = btk.GetTrackInfo(sourceTrack)
local newTracks = {}
reaper.SetOnlyTrackSelected(sourceTrack)
local nextTrackIndex = sourceTrackInfo.trackNumber1Based
for _, item in ipairs(sourceItems) do
btk.SelectOnlyItem(item)
reaper.InsertTrackInProject(0, nextTrackIndex, 0)
local newTrack = reaper.GetTrack(0, nextTrackIndex)
btk.SetTrackInfo(newTrack, {
name = sourceTrackInfo.name
})
newTracks[#newTracks + 1] = newTrack
nextTrackIndex = nextTrackIndex + 1
reaper.MoveMediaItemToTrack(item, newTrack)
reaper.SetMediaItemPosition(item, firstItemInfo.position, false)
end
btk.SelectOnlyTracks(newTracks)
reaper.ReorderSelectedTracks(sourceTrackInfo.trackNumber1Based, 1)
btk.SelectOnlyTracks(savedSelectedTracks)
reaper.SetEditCurPos(savedCursorPosition, 0, 0)
end)