Get Item State, misunderstanding?

If you have a question or need help, this is the place to be.
Post Reply
cvben
Posts: 38
Joined: Wed Jun 07, 2017 8:30 pm

Get Item State, misunderstanding?

Post by cvben » Sun Aug 19, 2018 9:40 pm

I was wanting to toy around with the Get item state event, but I'm clearly doing something wrong because Eventghost hates the following:

Specifically the python code

Code: Select all

<?xml version="1.0" encoding="UTF-8" ?>
<EventGhost Version="WIP-2018.06.26-10.32.22">
    <Folder Name="State eval" XML_Guid="{2D7CEC56-67DB-4C59-A675-7CD30D67DA2B}" Expanded="True">
        <Folder Name="test item" id="715" Enabled="False" XML_Guid="{4E243B0C-9FC9-40BB-904C-D1738CED22AB}" />
        <Macro Name="Get State: test item" XML_Guid="{8F5BB7C7-CC03-4DF3-BB59-0122E1EE027B}" Expanded="True">
            <Event Name="Keyboard.Shift" XML_Guid="{EB74AE0A-9FDD-4705-A07A-D1BDD43A7AC7}" />
            <Action XML_Guid="{5B79ABE7-25B6-42B3-A4CB-A2C2ACDA5F3B}">
                EventGhost.GetItemState(XmlIdLink(715))
            </Action>
            <Action XML_Guid="{1F4FE23D-9F39-4389-81F4-EB258E587813}">
                EventGhost.PythonScript(u"if eg.result = true:\n    print('enabled')\n    elif eg.result = false:\n        print('disabled')")
            </Action>
        </Macro>
    </Folder>
</EventGhost>
I just want to evaluate whether an item is enabled or disabled (what I at least think this is meant for).

User avatar
kgschlosser
Site Admin
Posts: 4623
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: Get Item State, misunderstanding?

Post by kgschlosser » Mon Aug 20, 2018 2:32 am

well first off you have a syntax error in the python script.

I am going to give you a quick rundown on python syntax.

Code: Select all

if eg.result = true:
    print('enabled')
    elif eg.result = false:
        print('disabled')

unlike C or C++ python cares a whole lot about indenting and spaces. it is how it knows what to run and when.

Above is your python script

there are several things wring but we are going to first start off with the indenting, indenting can be any consistent use of 2+ spaces or the use of tabs.
In the world of EG we prefer to use 4 spaces because it doesn't make a very large tab arrow when using an IDE and 4 is just the perfect amount to be able to skim over code and be able to read how it is processed.

ok so back to the indenting.

it would appear as tho you intended the elif to be on the same indent level as the if statement. and if that is not the case you cannot start an if elif else chain with an elif. it must always start with an if

Code: Select all

if this:
    run some code
elif that:
    run some code
else:
    run some code

ok so now that we established the importance of indenting

Code: Select all

if eg.result = true:
    print('enabled')
elif eg.result = false:
    print('disabled')
unlike C or C++ boolean objects are titled. so it would be

Code: Select all

true = True
false = False

ok so onto the next step

Code: Select all

if eg.result = True:
    print('enabled')
elif eg.result = False:
    print('disabled')

equality testing. using a single "=" sign is for setting/declaring a variable.

== equal to
!= not equal to
> greater then
>= greater then or equal to
< less then
<= less than or equal to

there are many more operators but this gives the basic idea. the purpose of the operator is to return a boolean answer.

Code: Select all

test1 = 1
test2 = 1
test3 = 2

print test1 == test2

print test2 == test3

print test1 == test2 == test3

the last print statement above is going to print False. chaining expressions when compiled actually is the same as this

Code: Select all

print test1 == test2 and test2 == test3
it simply saves the fingers some work.

Code: Select all

if eg.result == True:
    print('enabled')
elif eg.result == False:
    print('disabled')
using == is not the wrong thing to do above. it is not the "Pythonic" thing to do.

Code: Select all

if eg.result is True:
    print('enabled')
elif eg.result is False:
    print('disabled')

ok so now the last thing is the print statement it's self.

In python 2 print is not a function call. no need to wrap the passed parameter with ()'s

Code: Select all

if eg.result is True:
    print 'enabled'
elif eg.result is False:
    print 'disabled'


Now to really tweak the bean a bit lets make this a one liner for the sake of being complete in this little tutorial.

Code: Select all

print 'enabled' if eg.result is True else 'disabled' if eg.result is False else '???'
when making this a one liner you cannot use elif. we also have to compete the if else chain. we can't leave it open ended as you can using multi line. which ends up as a do nothing scenario. in this case we always want to print something so we need to give the print statement something to output.
If you like the work I have been doing then feel free to Image

User avatar
kgschlosser
Site Admin
Posts: 4623
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: Get Item State, misunderstanding?

Post by kgschlosser » Mon Aug 20, 2018 2:37 am

and you are correct about the use of the Get Item State action. it does test if an item is enabled or disabled and it will set eg.result to True or False

NOTE... if you copy and paste an action into a python script and you see XmlIdLink in it. you cannot use this action as python code. it can only be used as an item in the tree.


NOTE... the Dump Result to Log action does exactly that. it prints off the contents of eg.result to the log. so if you were to place this after the Get Item State action it would print True or False to the log. depending if the item you have selected is enabled or disabled.
If you like the work I have been doing then feel free to Image

cvben
Posts: 38
Joined: Wed Jun 07, 2017 8:30 pm

Re: Get Item State, misunderstanding?

Post by cvben » Tue Aug 21, 2018 2:55 am

After seeing your response and reading my code I honestly have no idea wtf I was doing. :shock:
My only excuse is that I've not written python since last year, but holy crow that was bad.

(Thankfully eg takes care of most of that troublesome code without me ever lifting a finger)

User avatar
kgschlosser
Site Admin
Posts: 4623
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: Get Item State, misunderstanding?

Post by kgschlosser » Tue Aug 21, 2018 5:24 am

no worries m8, I do the same thing when i look at my code. especially if it was something i wrote a long while ago. I also know how it is if you shift back and forth between languages. It is very easy to get things all mixed up.

The tutorial is also for any others out there trying to learn python. that is why i really explained in detail what was going on. I do like to explain each and every single step so any beginners out there can read it and fully understand what exactly is going on. the power of EG really gets unlocked with it's Python script action. and most times all is needed is a simple "if elif else" code block to do what is wanted. so this was a great example to really break it down.

It looks like you knew what you were doing but for a different language closely resembling C or C++
If you like the work I have been doing then feel free to Image

cvben
Posts: 38
Joined: Wed Jun 07, 2017 8:30 pm

Re: Get Item State, misunderstanding?

Post by cvben » Fri Aug 24, 2018 5:30 pm

kgschlosser wrote:
Tue Aug 21, 2018 5:24 am
It looks like you knew what you were doing but for a different language closely resembling C or C++
Guilty, I am actually surprised I didn't include all the endif's. I was actually toying around with VoiceMacro as an alternative to VoxCommando (never had much luck with Vox). It uses something along those lines. Decent little program btw, if a bit over/under engineered (can't really decide which)

Post Reply