Notice: This forum has been recovered from an old backup, so some content, links, and dates may be outdated. The forum is currently read-only while we restore sign-in and registration functionality. Details

If you find this forum valuable and would like to help keep it online, donations to help cover hosting and domain costs are greatly appreciated, but never expected. You can support the forum through Buy Me a Coffee or Ko-fi. Thank you for helping preserve the EventGhost community.

How to use Event Payload as Action Parameter

If you have a question or need help, this is the place to be.
Post Reply
SamWest
Posts: 8
Joined: Thu Dec 06, 2012 10:26 am

How to use Event Payload as Action Parameter

Post by SamWest »

I'm trying to write a plugin which will take my System.Volume's payload (ie the volume percentage) and pass it as a parameter to an Action in my plugin (so it will set my receiver's volume to the same level).

For example, I want to get the '28.00' bit from an event like:
System.Volume '28.00'
And call
MyPlugin.SetVolumeAbsolute(28)

I'm using the Python and XML code below, but getting this error.
TypeError: __call__() takes exactly 2 arguments (1 given)

Do I need to pass a reference to self into __call__(self, volume) or something? If so, how do I do that from the XML script?

My Action looks like this:

Code: Select all

class SetVolumeAbsolute(eg.ActionBase):
   ...
    def __call__(self, volume):
        return self.plugin.setVolumeAbsolute(volume)

    def Configure(self, volume=25):
        panel = eg.ConfigPanel(self)
        valueCtrl = panel.SpinIntCtrl(volume, min=0, max=100)
        panel.AddLine("Set absolute volume to", valueCtrl)
        while panel.Affirmed():
            panel.SetResult(valueCtrl.GetValue())
And my XML like this:

Code: Select all

<Macro Name="MyPlugin: Set Absolute Volume" Expanded="True">
        <Event Name="System.Volume" />
        <Action>
            MyPlugin.SetVolumeAbsolute(int(round(eg.event.payload)))
        </Action>
    </Macro>

Thanks,
Sam.
User avatar
Pako
Plugin Developer
Posts: 2294
Joined: Sat Nov 11, 2006 1:31 pm
Location: Czech Republic
Contact:

Re: How to use Event Payload as Action Parameter

Post by Pako »

When you use panel.SpinIntCtrl, so you can not use the eg.event.payload
[only if you use eg.plugins.MyPlugin.SetVolumeAbsolute(...) in the Python Script action].
But I recently created a new control: eg.SmartSpinIntCtrl.
You can see how to use it for example in the AIMP plugin.
Look for the set_volume class (from line 496).

Pako
SamWest
Posts: 8
Joined: Thu Dec 06, 2012 10:26 am

Re: How to use Event Payload as Action Parameter

Post by SamWest »

Sweet, thanks for that Pako, the SmartSpinIntCtrl got it working :)
I'll post the plugin in a new thread when I've tested/polished it a bit.

If anyone is interested in doing the same thing, here's my working plugin code:

Code: Select all

class SetVolumeAbsolute(eg.ActionBase):
    ...
    def __call__(self, volume):
        return self.plugin.setVolumeAbsolute(volume)
        
    def Configure(self, volume=25):
        panel = eg.ConfigPanel(self)
        valueCtrl = eg.SmartSpinIntCtrl(panel, -1, 0, min=0)
        panel.AddLine("Set absolute volume to", valueCtrl)
        while panel.Affirmed():
            panel.SetResult(valueCtrl.GetValue())
And the Macro that passes the System.Volume payload to the Action:

Code: Select all

<Macro Name="MyPlugin: Set Absolute Volume to 0" Expanded="True">
        <Event Name="System.Volume" />
        <Action>
            EventGhost.PythonCommand(u'eg.plugins.MyPlugin.SetVolumeAbsolute(float(eg.event.payload))')
        </Action>
    </Macro>
-Sam.
User avatar
Pako
Plugin Developer
Posts: 2294
Joined: Sat Nov 11, 2006 1:31 pm
Location: Czech Republic
Contact:

Re: How to use Event Payload as Action Parameter

Post by Pako »

That is not correct.
When you're doing it in such a way, then you can use straight wx.TextCtrl instead of eg.SmartSpinIntCtrl and even then it would not be quite correct.
First of all, there are missing eg.ParseString() and curly braces.
If you want the user to remain possibility to enter a value using SpinInt, then it should look something like this:

Code: Select all

class SetVolumeAbsolute(eg.ActionBase):
   ...
    def __call__(self, volume=0):
        volume = str(volume) if isinstance(volume,int) else eg.ParseString(volume)
        return self.plugin.setVolumeAbsolute(float(volume))

Code: Select all

<Macro Name="MyPlugin: Set Absolute Volume" Expanded="True">
        <Event Name="System.Volume" />
        <Action>
            MyPlugin.SetVolumeAbsolute(u'{eg.event.payload}')
        </Action>
    </Macro>
If you do not need to enter a value directly (but only as {eg.event.payload}), it is absolutely unnecessary to use eg.SmartSpinIntCtrl (as already mentioned). In that case, use wx.TextCtrl.

Pako
Post Reply