Go Back   Cockos Incorporated Forums > REAPER Forums > REAPER Q&A, Tips, Tricks and Howto

Reply
 
Thread Tools Display Modes
Old 08-12-2019, 03:43 PM   #1
puddi
Human being with feelings
 
puddi's Avatar
 
Join Date: Jun 2018
Posts: 375
Default Is there a script that can tell Reaper if a time selection is active or not?

I'm not sure if this makes any sense but basically I'm looking for a way to tell REAPER if a time selection is ON or OFF, like a toggle state. It could be useful in some cycle actions with the "IF" statements, so for example if a time selection is 'ON' then the action duplicates the selected area, else it duplicates the selected item (or whatever).

Thanks!
puddi is offline   Reply With Quote
Old 08-13-2019, 08:54 AM   #2
solger
Human being with feelings
 
solger's Avatar
 
Join Date: Mar 2013
Posts: 5,844
Default

Here's an example for checking the time selection state via LUA script:
Code:
starttime, endtime = reaper.GetSet_LoopTimeRange2(0, false, false, 0, 0, false)

if starttime == endtime then
  reaper.ShowMessageBox("No Time selection", "Info", 0)
else
  reaper.ShowMessageBox("Time selection exists", "Info", 0)
end

And another version which toggles the state of the action:
Code:
local self = ({reaper.get_action_context()})[4]
starttime, endtime = reaper.GetSet_LoopTimeRange2(0, false, false, 0, 0, false)

if starttime == endtime then
  reaper.SetToggleCommandState(0, self, 0)
else
  reaper.SetToggleCommandState(0, self, 1)
end
But I can't remember right now if it's also possible to use such a script as IF condition in a Cycle Action.
__________________
ReaLauncher
solger is offline   Reply With Quote
Old 08-13-2019, 08:58 AM   #3
Lokasenna
Human being with feelings
 
Lokasenna's Avatar
 
Join Date: Sep 2008
Location: Calgary, AB, Canada
Posts: 6,551
Default

Cycle actions can check the state of other actions that are registered as a toggle (i.e. the ones that show On/Off in the action list). I think you could have the cycle action run a script prior to the If, the script would update its own state in the list based on the time selection as above, and then the cycle action could check it for the If.

However, I've never used cycle actions or the API command above so I'm not sure exactly how to do it.
__________________
I'm no longer using Reaper or working on scripts for it. Sorry. :(
Default 5.0 Nitpicky Edition / GUI library for Lua scripts / Theory Helper / Radial Menu / Donate
Lokasenna is offline   Reply With Quote
Old 08-13-2019, 10:55 AM   #4
puddi
Human being with feelings
 
puddi's Avatar
 
Join Date: Jun 2018
Posts: 375
Default

Quote:
Originally Posted by solger View Post
Here's an example for checking the time selection state via LUA script:
Code:
starttime, endtime = reaper.GetSet_LoopTimeRange2(0, false, false, 0, 0, false)

if starttime == endtime then
  reaper.ShowMessageBox("No Time selection", "Info", 0)
else
  reaper.ShowMessageBox("Time selection exists", "Info", 0)
end

And another version which toggles the state of the action:
Code:
local self = ({reaper.get_action_context()})[4]
starttime, endtime = reaper.GetSet_LoopTimeRange2(0, false, false, 0, 0, false)

if starttime == endtime then
  reaper.SetToggleCommandState(0, self, 0)
else
  reaper.SetToggleCommandState(0, self, 1)
end
But I can't remember right now if it's also possible to use such a script as IF condition in a Cycle Action.
Nice, so it is possible! Now I just need to figure out how to implement it in to cycle actions since it seems like they can't register the toggle states of scripts.
puddi is offline   Reply With Quote
Old 09-16-2020, 07:19 AM   #5
Loulou92
Human being with feelings
 
Loulou92's Avatar
 
Join Date: Sep 2015
Location: Paris
Posts: 544
Default

Hello !

Have you managed to make the script work as a toggle ? Right now I'm not sure how to use this script.
Loulou92 is offline   Reply With Quote
Old 09-16-2020, 07:33 AM   #6
solger
Human being with feelings
 
solger's Avatar
 
Join Date: Mar 2013
Posts: 5,844
Default

Quote:
Originally Posted by Loulou92 View Post
Hello !

Have you managed to make the script work as a toggle ? Right now I'm not sure how to use this script.
Hi,

can you post more details about what you're trying to do?
__________________
ReaLauncher
solger is offline   Reply With Quote
Old 09-16-2020, 08:18 AM   #7
Loulou92
Human being with feelings
 
Loulou92's Avatar
 
Join Date: Sep 2015
Location: Paris
Posts: 544
Default

Hello ! I have a cycle action that works great, but if by mistake I launch it without a time selection, it deletes many items (it's supposed to work with a time selection, but sometimes I forget). I reviewed the action in detail, and there is no way around this, even with convoluted techniques using what's available in the Reaper action list, SWS and scripts.

The only way to avoid the action to be destructive without a time selection would be to have a check at the beginning of the cycle action :

If the next action is on :
"is there a time selection.lua"
it proceeds and do all the rest of the action
Else
No-op
End of conditional

With the script as is, sometimes the action is executed even if there is no time sel, it's not realiable, I don't know why.

Thanks !
xxxxxx

Edit : I'll add that usually, such problems (misbehaving custom actions that have side effects under some circumstances) are easily solved by a Ctrlr + z. The thing is... The deleted items are deleted on hidden tracks. So basically the damage is "invisible", and then this little problem can be a ticking bomb if I don't realize the action misbehaved and I go on with the work. This is why I try to find a way to have time selection as a toggle, to avoid catastrophy on my hidden tracks.

Last edited by Loulou92; 09-16-2020 at 09:07 AM.
Loulou92 is offline   Reply With Quote
Old 09-17-2020, 04:45 PM   #8
Loulou92
Human being with feelings
 
Loulou92's Avatar
 
Join Date: Sep 2015
Location: Paris
Posts: 544
Default

Well I guess I'll have to do without it then .
Loulou92 is offline   Reply With Quote
Old 09-18-2020, 01:56 PM   #9
Embass
Human being with feelings
 
Embass's Avatar
 
Join Date: Jan 2014
Posts: 923
Default

scripts can run actions from action list. just copy command ID from action list and paste to script.

Code:
-- lua script

-- run action from action list (if time selection exist)

-- copy command ID from action list
local sws_cycle_action_id = "_S&M_CYCLACTION_5" -- example

local command_id = reaper.NamedCommandLookup(sws_cycle_action_id)
if command_id == 0 then reaper.ShowConsoleMsg("Error: action does not exist.. ") return end -- exit

local time_sel_start, time_sel_end = reaper.GetSet_LoopTimeRange(false, false, 0, 0, false)

if time_sel_start ~= time_sel_end then
	-- if time selection exist run action
	reaper.Main_OnCommand(command_id, 0)
else 
	-- do nothing..
end

Last edited by Embass; 09-18-2020 at 02:53 PM.
Embass is offline   Reply With Quote
Old 09-18-2020, 02:48 PM   #10
Loulou92
Human being with feelings
 
Loulou92's Avatar
 
Join Date: Sep 2015
Location: Paris
Posts: 544
Default

That's super badass !!! I'm testing in a few minutes.
Loulou92 is offline   Reply With Quote
Old 09-18-2020, 02:55 PM   #11
Loulou92
Human being with feelings
 
Loulou92's Avatar
 
Join Date: Sep 2015
Location: Paris
Posts: 544
Default

Ok it works perfectly. Thanks Embass !!

Even if it's unrelated to what's being discussed here, I wonder : is it possible to have other conditions ?

example :
do the action only if an item is selected
do the action only if track is in group x

Anyway, that's out of curiosity, and potential use also I confess, but thanks anyway for the script shared . Will be used a lot !

Last edited by Loulou92; 09-18-2020 at 03:02 PM.
Loulou92 is offline   Reply With Quote
Old 09-22-2020, 10:16 AM   #12
solger
Human being with feelings
 
solger's Avatar
 
Join Date: Mar 2013
Posts: 5,844
Default

Quote:
Originally Posted by Loulou92 View Post
Even if it's unrelated to what's being discussed here, I wonder : is it possible to have other conditions ?

example :
do the action only if an item is selected
Have a look at the GetSelectedMediaItem function for this: https://www.extremraym.com/cloud/rea...ectedMediaItem

Code:
if reaper.GetSelectedMediaItem(0,0) then
    -- an item is selected
    ...
else
   -- no item selected
   ...
end

Quote:
Originally Posted by Loulou92 View Post
do the action only if track is in group x
Not sure, but maybe it's possible to get the group via the GetSetTrackGroupMembership function:
Or via the GROUP_FLAGS parameter of the GetTrackStateChunk function: You might also find some hints when looking at the code of the me2beats_Select all tracks in selected track groups script mentioned in this thread: https://forum.cockos.com/showthread.php?t=194910
__________________
ReaLauncher

Last edited by solger; 09-22-2020 at 11:06 AM.
solger is offline   Reply With Quote
Old 09-23-2020, 08:59 AM   #13
Loulou92
Human being with feelings
 
Loulou92's Avatar
 
Join Date: Sep 2015
Location: Paris
Posts: 544
Default

Ok, thank you very much !! I'll have a look this evening at the links you posted & hopefully it's going to work out fine !!!
Loulou92 is offline   Reply With Quote
Old 09-24-2020, 01:02 PM   #14
Loulou92
Human being with feelings
 
Loulou92's Avatar
 
Join Date: Sep 2015
Location: Paris
Posts: 544
Default

As of now I have sucesfully created the script that checks if an item is selected before doing an action, it's implemented in many of my custom action now, super cool ! COuld you please tell me what I should put in the code to make sure that the action works only if ONE item is selected, not 0 or not 2 and more ?
Loulou92 is offline   Reply With Quote
Old 09-25-2020, 10:58 AM   #15
solger
Human being with feelings
 
solger's Avatar
 
Join Date: Mar 2013
Posts: 5,844
Default

Quote:
Originally Posted by Loulou92 View Post
COuld you please tell me what I should put in the code to make sure that the action works only if ONE item is selected, not 0 or not 2 and more ?
Should be possible with the CountSelectedMediaItems function:

Code:
if reaper.CountSelectedMediaItems(0) == 1 then
    -- only one item is selected
    ...
else
   -- no or more than one item(s) selected
   ...
end

Another example (if you want to do different things depending on how many items are selected):
Code:
local selectedItemCount = reaper.CountSelectedMediaItems(0)

if selectedItemCount == 1 then
    -- only one item is selected
    ...
elseif selectedItemCount == 2 then
   -- two items are selected 
   ...
else
 -- no or more than two item(s) selected
end
And so on ...
__________________
ReaLauncher

Last edited by solger; 09-25-2020 at 11:08 AM.
solger is offline   Reply With Quote
Old 09-25-2020, 11:16 AM   #16
Loulou92
Human being with feelings
 
Loulou92's Avatar
 
Join Date: Sep 2015
Location: Paris
Posts: 544
Default

Ok Solger, thank you, I'll try all these leads this evening. I should start lua or Python, potential seems infinite compared to the logical operations available in the SWS extension...

xxxx
Loulou92 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 02:07 AM.


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