/
TJF Select Items within time selection.lua
114 lines (68 loc) · 3.02 KB
/
TJF Select Items within time selection.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
--@description TJF Select Items withing Time Selection on Selected Tracks
--@version 1.0
--@author Tim Farrell
--
--@about
-- #TJF Select Items withing Time Selection on Selected Tracks
-- Will select all items that start or end within the time selection on selected tracks
--
--@changelog
-- v1.0 - nothing to report
----------------------------------
-- DEBUG --
----------------------------------
function Msg(param) reaper.ShowConsoleMsg(tostring(param).."\n") end
----------------------------------
-- FUNCTIONS --
----------------------------------
function GetSelections() -- goes through each item on the selected tracks and determines if it falls within the time selection
local start_time, end_time = reaper.GetSet_LoopTimeRange2(0, false, false, 0, 0, false) -- Get start and end time selection value in seconds
if start_time ~= end_time then
local items = {}
for t=0, reaper.CountSelectedTracks(0)-1 do
local track = reaper.GetSelectedTrack(0,t)
for i=0, reaper.CountTrackMediaItems( track )-1 do
local item = reaper.GetTrackMediaItem( track, i)
local istart = reaper.GetMediaItemInfo_Value( item, "D_POSITION" )
local iend = istart + reaper.GetMediaItemInfo_Value( item, "D_LENGTH" )
if (istart >= start_time and istart <= end_time) or (iend >= start_time and iend <= end_time)
then
table.insert(items, item)
end--if
end--for
end--for
return items
else
return false
end
end--GetSelections
function CheckItems(items) -- checks if any items in the table are currently unselected. if any are unselected, returns true, otherwise, will return false
for i=1, #items do
if not reaper.IsMediaItemSelected( items[i] ) then return true end
end--for
return false
end
function ProcessItems(items, bool) -- loops through items and sets selected based on bool parameter
for i=1, #items do
reaper.SetMediaItemSelected( items[i], bool )
end
end
----------------------------------
-- MAIN --
----------------------------------
function Main()
reaper.SelectAllMediaItems( 0, false ) -- clears current item selections
local items = GetSelections() -- get items that fall within the time range
if items then
ProcessItems(items, CheckItems(items)) -- select (or deselct) items
end
end--Main()
----------------------------------
-- CALL SCRIPT --
----------------------------------
reaper.Undo_BeginBlock()
reaper.PreventUIRefresh(1) -- uncomment only once script works
Main()
reaper.PreventUIRefresh(-1) -- uncomment only once script works
reaper.UpdateArrange()
reaper.Undo_EndBlock("TJF Select Items withing Time Selection on Selected Tracks", -1)