GP SoftwareTwitter
Opus FAQsManualCommandsObjects

DO11: Using @set to "toggle" commands?


#1

The brief description of @set in the release thread has me wondering if it can be used to create a toggle button that will run one of external two commands depending on the value of a variable set by clicking the button?

Specifically, I use NirCMD to switch between speakers and HDMI as my active audio device using two different commands set to global hotkeys. Can I now put these two commands into a toggle button and use a variable to help DOpus “remember” the state of the button? In other words, I hope to be able to click once to run one command and toggle the button ON, then click again to run the second command and toggle the button OFF.

Thanks!


DO11: [FR] Test @set variables with @toggle?
DO11: Using @set to toggle Windows boot configurations?
#2

Yes, that type of thing is one of the problems the new persistent variables aim to solve.

You may also be able to make a script which queries the current default audio device so that you don’t have to use the variable at all (and then the variable will never be out of sync with reality, like would happen if something else changed the audio device). I’m not sure though. WMI has some ways to query audio devices, and can be accessed via scripts, but I don’t know if it lets you tell which device is the current default. Might be worth looking into though, if you’re OK with a bit of scripting.


#3

That sounds like it’s a little beyond my current capabilities :slight_smile:

Once the manual is ready I’ll try the variable method. I can’t think of any time the button would get out of sync…


#4

Here’s how to do the toggle, with the variable being saved between reboots:

(I’ve called the variable HDMIAudio.)

@ifset:$glob!:HDMIAudio @set glob!:HDMIAudio <put one of your NirCMD commands here> @ifset:else @set glob!:HDMIAudio=1 <put your other NirCMD command here>


#5

Cool, it works great!

Now, is it possible to make it a proper toggle button so I can tell when my audio is switched over to my TV?


#6

Don’t think there’s a way for it to indicate its state, at least not yet.


#7

I found I can indicate its state by also toggling sounds. Since I don’t use them, I will never notice if they’re on or off. Adding this code to my button does the trick!

It’s a bit of a hack of course, and if I need to do something similar to another button I’ll have to find another setting that I don’t use. But I have a feeling that you guys are working on this problem, so I’m not worried :slight_smile:


#8

Leo, I’m confused about the syntax in your example. It looks a bit different from the manual, where you can test a value, for instance @ifset:DUAL=on, but not just @ifset:DUAL

Line 1: @ifset:$glob!:HDMIAudio
Are you just testing if $glob!:HDMIAudio exists? (Regardless of value?)
Or testing if it exists AND is set to 1?

Line 2: @set glob!:HDMIAudio
What does this do to the variable?
In contract , below you set it to 1: @set glob!:HDMIAudio=1

Thanks in advance!


#9

From the release notes:

The @ifset directive can now test if a variable has previously been set by the @set directive (although it can’t actually test what the value is - only if it exists or not). For example, @ifset:$glob:foo would test if a global variable called foo had been set.


#10

So following up on playful’s question…

Does @set glob!:HDMIAudio=1 set the variable to 1, and @set glob!:HDMIAudio set the variable to 0?


#11

@set glob!:HDMIAudio=0 would set it to 0.

@set glob!:HDMIAudio removes the variable entirely. (If it did anything else, the @ifset stuff in my example wouldn’t have worked, since @ifset can only test if a variable exists or not and cannot test what the variable’s value is. So we need a way to create the variable (with some value; it doesn’t matter what but 1 is used in my example) and another way to destroy the variable.)


#12

Crystal-clear, Leo. Thanks. :slight_smile:


#13

@newguy, thanks to Leo and Jon it looks like you can do away with Set SOUNDS=Toggle and use something like:

[code]@toggle:if $glob!:HDMIAudio

@ifset:$glob!:HDMIAudio
@set glob!:HDMIAudio

@ifset:else
@set glob!:HDMIAudio=1
[/code]


#14

I saw the notes in the new beta and meant to try it. I don’t feel like restarting my system right now, but I’ll install the new version soon and try it out :slight_smile: