Old 02-11-2019, 02:41 PM   #1
andyp24
Human being with feelings
 
andyp24's Avatar
 
Join Date: Mar 2016
Posts: 1,239
Default Toolbar Icon toggle with script

Hi all

This feels like it should be simple. I have a defer script which runs in the background doing stuff. I'd like to create a toolbar icon which changes color/picture when the script is running, compared to when it's off. So I can click it to run / stop the script, or if the script is run/stopped another way (eg by a keyboarc command) the icon will show me whether it's running or not.

I've tried searching, but can't see a noob-level explanation! :-)

Thanks all
Andy
andyp24 is offline   Reply With Quote
Old 02-11-2019, 02:46 PM   #2
andyp24
Human being with feelings
 
andyp24's Avatar
 
Join Date: Mar 2016
Posts: 1,239
Default

Edit - I just found the SWS Dummy Toggles.

Is the point of those that you just assign the toolbar button to SWS/Dummy Toggle 1 (for example), and then in the script run that action on start/exit?

A.
andyp24 is offline   Reply With Quote
Old 02-11-2019, 03:06 PM   #3
nofish
Human being with feelings
 
nofish's Avatar
 
Join Date: Oct 2007
Location: home is where the heart is
Posts: 12,096
Default

Dummy toggles were originally added to do cycle actions with actions that don't have a 'real' toggle state I think.

https://forum.cockos.com/showpost.php?p=1090262

But as mentioned there, they can go out of sync.

The more 'proper' way to support a toggle toolbar with a defer script is to do something like this I think:

at script start:
Code:
-- set toolbar button to on
is_new_value,filename,section_ID,cmd_ID,mode,resolution,val = reaper.get_action_context()
reaper.SetToggleCommandState(section_ID, cmd_ID, 1)
reaper.RefreshToolbar2(section_ID, cmd_ID)
and when script stops:
https://www.extremraym.com/cloud/rea...oc/#lua_atexit


Code:
-- set toolbar button to off
reaper.SetToggleCommandState(section_ID, cmd_ID, 0);
reaper.RefreshToolbar2(section_ID, cmd_ID);
nofish is offline   Reply With Quote
Old 02-11-2019, 03:54 PM   #4
andyp24
Human being with feelings
 
andyp24's Avatar
 
Join Date: Mar 2016
Posts: 1,239
Default

Thanks nofish

That code looks like it works for a script toggling a native action which already has a toolbar icon, but how does it work when I need to create a button/icon on a custom toolbar and assign it to my defer script?

A.
andyp24 is offline   Reply With Quote
Old 02-11-2019, 07:00 PM   #5
nofish
Human being with feelings
 
nofish's Avatar
 
Join Date: Oct 2007
Location: home is where the heart is
Posts: 12,096
Default

Just assign your toolbar button to the defer script with above code in it and it should work.

edit:
More complete minimal example:

Code:
-- defer demo
_, _, sectionID, cmdID, _, _, _ = reaper.get_action_context()
-- set toggle state to on
reaper.SetToggleCommandState(sectionID, cmdID, 1);
reaper.RefreshToolbar2(sectionID, cmdID);

function Main() -- your defer code
  reaper.ShowConsoleMsg("defer script running...\n")
  reaper.defer(Main)
end

Main()

function DoAtExit()
  -- set toggle state to off
  reaper.SetToggleCommandState(sectionID, cmdID, 0);
  reaper.RefreshToolbar2(sectionID, cmdID);
end

reaper.atexit(DoAtExit)


First time you stop the script via toolbar, Reaper asks about terminating instances. Click yes and remember answer.

Last edited by nofish; 02-11-2019 at 07:27 PM.
nofish is offline   Reply With Quote
Old 02-12-2019, 03:16 AM   #6
andyp24
Human being with feelings
 
andyp24's Avatar
 
Join Date: Mar 2016
Posts: 1,239
Default

OK thanks a lot.

What does the GetActionContext function actually do? I'm sure it works as you say, but I would like to understand how it knows what toolbar / icon to affect...

Cheers
Andy
andyp24 is offline   Reply With Quote
Old 02-12-2019, 06:21 AM   #7
nofish
Human being with feelings
 
nofish's Avatar
 
Join Date: Oct 2007
Location: home is where the heart is
Posts: 12,096
Default

In this case it returns in which action section (Main, MIDI editor etc.) the script is registered ('sectionID) and which command ID it has (command IDs can also be seen in action list) . With this info the toolbar button the script is assigned to can be toggled from the script with SetToggleCommandState().



edit:
I think get_action_context() rather gets command ID number (which I think is generated at runtime) than command ID.

Last edited by nofish; 02-14-2019 at 09:31 AM.
nofish is offline   Reply With Quote
Old 02-13-2019, 12:33 PM   #8
andyp24
Human being with feelings
 
andyp24's Avatar
 
Join Date: Mar 2016
Posts: 1,239
Default

Gotcha. Thanks very much.
andyp24 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 11:01 PM.


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