Help converting a simple applescript to Python

This is a legacy forum which is locked for new topics. New topics should be started in one of the other forums under Extending Indigo
Forum rules
This is a legacy forum which is locked for new topics. New topics should be started in one of the other forums under Extending Indigo
User avatar
matt (support)
Site Admin
Posts: 21542
Joined: Mon Jan 27, 2003 1:17 pm
Location: Texas
Contact:

Re: Help converting a simple applescript to Python

Post by matt (support) »

Okay, so it still doesn't work on the original Mac then. I'm not sure what could be different. Have you installed anything python related on that system?

For the updating problem, try each one of these lines individually (and by themselves -- no IF conditional) to see which ones work (and copy/paste any errors you see):

Code: Select all

indigo.variable.updateValue(717445627, "foo1")

Code: Select all

indigo.variable.updateValue("yourVariableNameHere", "foo2")

Code: Select all

import datetime
indigo.variable.updateValue("yourVariableNameHere", str(datetime.datetime.now()))
Image
User avatar
bob
Posts: 500
Joined: Wed Jun 14, 2006 7:55 am

Re: Help converting a simple applescript to Python

Post by bob »

Matt,

I'll get back to you when I next get a chance to work on this. I don't have as much time for this as I used to. I thought starting with a simple script was going to be easy. Anyway.... thanks much for your assistance and if I stumble upon the solution in the meantime I'll be sure to post it. I'll get back to you with the results of your test when I can.

best,

bob
User avatar
bob
Posts: 500
Joined: Wed Jun 14, 2006 7:55 am

Re: Help converting a simple applescript to Python

Post by bob »

Matt,

OK I found something. This is what I have found so far;

Yesterday on the airbook I overwrote the trigger's applescript with the python script and got errors when I tried to compile it (I did select it to a python script). This morning I made a new trigger with everything the same but as it was a new trigger it did not previously have an applescript. I pasted in the python script and bingo it worked as advertised. (this is on a database imported from 4.0)

Next I created a new empty 5.0 database and made a trigger with an applescript. I then overwrote the applescript with a python script and it worked OK.

From this it looks like if you have an imported database from 4.0, a trigger can stay stuck on applescript if you try to change it to a python script. I tried duplicating the script then pasting in the python script but I got the same error. I had to create a new trigger to get it to work. I am going to try this on other triggers with applescripts.
User avatar
jay (support)
Site Admin
Posts: 19232
Joined: Wed Mar 19, 2008 11:52 am
Location: Austin, Texas
Contact:

Re: Help converting a simple applescript to Python

Post by jay (support) »

Just wanted to point out that I suggested that you delete the old action and create a new one back on my last post on the first page of the thread. That would have solved the problem.

We can't stress enough that following our guidance EXACTLY will always be the fastest path to problem resolution.
Jay (Indigo Support)
Twitter | Facebook | LinkedIn
User avatar
bob
Posts: 500
Joined: Wed Jun 14, 2006 7:55 am

Re: Help converting a simple applescript to Python

Post by bob »

Jay,

Sorry, I guess I missed that.

Why is it necessary to delete the Action? Is it only when changing from an Applescript to a Python script that this is necessary? Is the Applescript not being completely overwritten in the database when you switch to a Python script?
User avatar
matt (support)
Site Admin
Posts: 21542
Joined: Mon Jan 27, 2003 1:17 pm
Location: Texas
Contact:

Re: Help converting a simple applescript to Python

Post by matt (support) »

It shouldn't be necessary. We aren't able to reproduce the problem, so if you can provide steps and an example AppleScript that does that would be helpful.
Image
User avatar
jay (support)
Site Admin
Posts: 19232
Joined: Wed Mar 19, 2008 11:52 am
Location: Austin, Texas
Contact:

Re: Help converting a simple applescript to Python

Post by jay (support) »

To be honest it was a way to ensure that all of the text in the script area actually did get deleted. When we tell someone to replace the text in an area they don't always make sure that EVERYTHING in the area is gone. I suspect this was your problem all along - you just didn't get all the text (perhaps there was some unprintable character or something that got into it accidentally or a leftover from the AppleScript). When replacing text in a text field, it's always recommended that you do a Command-A - which will select everything (including unprintable characters) and then hit delete. That ensures that the area is completely empty.

It shouldn't be necessary to do the action delete/create - we completely replace the text from the dialog in the database when it's saved. It's possible there's a bug in that dialog - which is why we need a way to reproduce the problem.
Jay (Indigo Support)
Twitter | Facebook | LinkedIn
User avatar
matt (support)
Site Admin
Posts: 21542
Joined: Mon Jan 27, 2003 1:17 pm
Location: Texas
Contact:

Re: Help converting a simple applescript to Python

Post by matt (support) »

We've found steps that reproduce the problem and will have it fixed in Indigo 51.0. The workaround is to simply select all and press delete when switching between AppleScript and Python script types.
Image
User avatar
bob
Posts: 500
Joined: Wed Jun 14, 2006 7:55 am

Re: Help converting a simple applescript to Python

Post by bob »

Matt,

For some reason I didn't get notification of your reply but anyway I a happy you found the problem.

I am trying to execute this script;

Code: Select all

import xml.etree.cElementTree as ET

#location of the file to be parsed
content = ET.ElementTree(file="/Users/abook/Desktop/tempalert.xml")

ports = content.find("ports")

#get all the port elements
for port in ports.findall('port'):
    
    if port.get('name') == 'Port 1':

        currentTemp = port.find("condition/currentReading")
        #print currentTemp.text
        indigo.variable.updateValue("tempAlert1_", currentTemp)

    elif port.get('name') == 'Port 2':

        currentTemp = port.find("condition/currentReading")
        #print currentTemp.text        
        indigo.variable.updateValue("tempAlert2_", currentTemp)
But I get this error;
$ indigohost -x /Users/abook/Desktop/workingcopy.py
workingcopy.py: invalid syntax
around line 1 - "import xml.etree.cElementTree as ET"

But if I run this below it runs OK.

Code: Select all

import xml.etree.cElementTree as ET

#location of the file to be parsed
content = ET.ElementTree(file="/Users/abook/Desktop/tempalert.xml")

ports = content.find("ports")

#get all the port elements
for port in ports.findall('port'):
    
    if port.get('name') == 'Port 1':

        currentTemp = port.find("condition/currentReading")
        print currentTemp.text
        #indigo.variable.updateValue("tempAlert1_", currentTemp)

    elif port.get('name') == 'Port 2':

        currentTemp = port.find("condition/currentReading")
        print currentTemp.text        
        #indigo.variable.updateValue("tempAlert2_", currentTemp)

Code: Select all

$ python /Users/abook/Desktop/workingcopy.py
73.2
950.2
User avatar
matt (support)
Site Admin
Posts: 21542
Joined: Mon Jan 27, 2003 1:17 pm
Location: Texas
Contact:

Re: Help converting a simple applescript to Python

Post by matt (support) »

This is the same problem. The line endings on your .py file are probably Mac (CR+LF) but should be Unix (LF only).
Image
User avatar
jay (support)
Site Admin
Posts: 19232
Joined: Wed Mar 19, 2008 11:52 am
Location: Austin, Texas
Contact:

Re: Help converting a simple applescript to Python

Post by jay (support) »

Most good text editors will fix it for you based on the file type.

BTW, it compiles fine for me:

Code: Select all

FatMac:temp jay$ indigohost -x ./test.py
test.py: [Errno 2] No such file or directory: '/Users/abook/Desktop/tempalert.xml'
Exception Traceback (most recent call shown last):

     test.py, line 4, at top level
     File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/xml/etree/ElementTree.py", line 546, in __init__
       self.parse(file)
     test.py, line 21, in parse
IOError: [Errno 2] No such file or directory: '/Users/abook/Desktop/tempalert.xml'
It compiled fine and only threw when it ran and couldn't find your xml file on my system.
Jay (Indigo Support)
Twitter | Facebook | LinkedIn
Locked

Return to “Extending Indigo with Plugins and Python”