Run multi-line Python script within AppleScript?

Posted on
Sat Aug 13, 2022 9:31 pm
billearl offline
Posts: 26
Joined: Aug 16, 2005

Run multi-line Python script within AppleScript?

This has been driving me crazy. I want to run a multi-line Python script within an AppleScript script (no .py file). I'm using Indigo 7.4.

Specifically, I want to to check the onState of a device, then turn that device on or off accordingly, similar to toggle, but conditional on the onState.
If onState is "on", turn the device off, and vice versa. Sounds simple to me, but I can't figure how to do it. I'm a "lite" user of Python.

Can this be done? If so, how?

Posted on
Sun Aug 14, 2022 7:34 am
FlyingDiver offline
User avatar
Posts: 7221
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Run multi-line Python script within AppleScript?

I have no idea if what you're trying to do is possible, but I recommend you don't even try. Just convert the AppleScript to Python and be done with it. You're going to have to sooner or later, so might as well do it now.

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Sun Aug 14, 2022 8:01 am
howartp offline
Posts: 4559
Joined: Jan 09, 2014
Location: West Yorkshire, UK

Re: Run multi-line Python script within AppleScript?

I saw your post this morning as I was rushing out and thought "nope, i'll let FlyingDiver answer that"...

I completely agree with what he's said, just don't even try.

In terms of python, the following single line will do what you need if you really want it in a single line:

Code: Select all
indigo.device.turnOff(796740025) if indigo.devices[796740025].states['onOffState'] else indigo.device.turnOn(796740025)

The syntax is:
some_expression if condition else other_expression

Turn it off if it is on else turn it on


A better multiline approach would be:
Code: Select all
myDevID = 796740025
devIsOn = indigo.devices[myDevID].states['onOffState']
if (devIsOn):
   indigo.device.turnOff(myDevID)
else:
   indigo.device.turnOn(myDevID)

Posted on
Sun Aug 14, 2022 1:34 pm
billearl offline
Posts: 26
Joined: Aug 16, 2005

Re: Run multi-line Python script within AppleScript?

Thanks very much. I want to use AppleScript for this, run from (Many Tricks) Butler, which does not run Python scripts directly. I just assumed it would be straightforward to encapsulate a multi-line python script in AppleScript, but apparently not. I will try running a separate ".py" file from AppleScript then.

Follow-up:

This works,
Code: Select all
do shell script "/usr/local/bin/indigo-host -x '/Users/billearl/Desktop/fan.py'"

where fan.py contains,
Code: Select all
myDevID = 100076704
devIsOn = indigo.devices[myDevID].states['onOffState']
if (devIsOn):
   indigo.device.turnOff(myDevID)
else:
   indigo.device.turnOn(myDevID)

Posted on
Sun Aug 14, 2022 5:04 pm
billearl offline
Posts: 26
Joined: Aug 16, 2005

Re: Run multi-line Python script within AppleScript?

Now, I just want to find how to pass the device ID from AppleScript to Python so I can use just one ".py" file to control different devices.

Any ideas?

Posted on
Sun Aug 14, 2022 5:41 pm
ryanbuckner offline
Posts: 1080
Joined: Oct 08, 2011
Location: Northern Virginia

Re: Run multi-line Python script within AppleScript?

billearl wrote:
Now, I just want to find how to pass the device ID from AppleScript to Python so I can use just one ".py" file to control different devices.

Any ideas?


Untested by I think you can just do :

Code: Select all
do shell script "/usr/local/bin/indigo-host -x '/Users/billearl/Desktop/fan.py ' + dev_id"


then:

Code: Select all
# best to test if argv[1] exits and trap or default
import sys
myDevID = sys.argv[1]
devIsOn = indigo.devices[myDevID].states['onOffState']
if (devIsOn):
   indigo.device.turnOff(myDevID)
else:
   indigo.device.turnOn(myDevID)

Posted on
Sun Aug 14, 2022 6:48 pm
billearl offline
Posts: 26
Joined: Aug 16, 2005

Re: Run multi-line Python script within AppleScript?

Thanks. Unfortunately, I can't seem to pass a variable from AppleScript to Python that way.

I tried AppleScript,
Code: Select all
set dev_id to 100076704
do shell script "/usr/local/bin/indigo-host -x '/Users/billearl/Desktop/fan.py' " & dev_id

with Python ("fan.py"),
Code: Select all
import sys
myDevID = sys.argv[1]
devIsOn = indigo.devices[myDevID].states['onOffState']
if (devIsOn):
   indigo.device.turnOff(myDevID)
else:
   indigo.device.turnOn(myDevID)

but the device did not respond.

Another test using,
Code: Select all
import sys
print(sys.argv)

seemed to indicate nothing was passed from AppleScript to Python.

Posted on
Sun Aug 14, 2022 6:51 pm
FlyingDiver offline
User avatar
Posts: 7221
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Run multi-line Python script within AppleScript?

Try:
Code: Select all
set dev_id to 100076704
do shell script "/usr/local/bin/indigo-host -x '/Users/billearl/Desktop/fan.py " & dev_id & "'"


Need to get the device ID into the single quoted string.

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Sun Aug 14, 2022 7:29 pm
billearl offline
Posts: 26
Joined: Aug 16, 2005

Re: Run multi-line Python script within AppleScript?

That results in an error: (from Script Debugger)

Traceback (most recent call last):
File "/usr/local/bin/indigo-host", line 149, in <module>
main(sys.argv[1:])
File "/usr/local/bin/indigo-host", line 88, in main
args = parser.parse_args()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/argparse.py", line 1705, in parse_args
args, argv = self.parse_known_args(args, namespace)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/argparse.py", line 1737, in parse_known_args
namespace, args = self._parse_known_args(args, namespace)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/argparse.py", line 1943, in _parse_known_args
start_index = consume_optional(start_index)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/argparse.py", line 1883, in consume_optional
take_action(action, args, option_string)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/argparse.py", line 1795, in take_action
argument_values = self._get_values(action, argument_strings)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/argparse.py", line 2235, in _get_values
value = self._get_value(action, arg_string)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/argparse.py", line 2264, in _get_value
result = type_func(arg_string)
File "/usr/local/bin/indigo-host", line 44, in valid_path
raise Exception("The specified path '{}' doesn't exist".format(in_path))
Exception: The specified path '/Users/billearl/Desktop/fan.py 100076704' doesn't exist

Posted on
Sun Aug 14, 2022 7:48 pm
FlyingDiver offline
User avatar
Posts: 7221
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Run multi-line Python script within AppleScript?

You've got an errant colon in that line.

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Sun Aug 14, 2022 9:05 pm
billearl offline
Posts: 26
Joined: Aug 16, 2005

Re: Run multi-line Python script within AppleScript?

So it would appear from the error message, "main(sys.argv[1:])", but there are no colons in my code, except for the two colons in the Python if - else conditional. I can't explain why the error message contains what appears to be an extraneous colon.

Posted on
Mon Aug 15, 2022 6:02 am
FlyingDiver offline
User avatar
Posts: 7221
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Run multi-line Python script within AppleScript?

Yeah, the problem is actually that the indigo-host script only accepts a valid path for the -x option. No additional arguments allowed.

There's probably some other way to skin this cat, but they're going to be more convoluted.

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Mon Aug 15, 2022 10:43 am
billearl offline
Posts: 26
Joined: Aug 16, 2005

Re: Run multi-line Python script within AppleScript?

How do I get a man page for indigo-host? I'd like to see the options available. Thanks.

Posted on
Mon Aug 15, 2022 11:28 am
DaveL17 offline
User avatar
Posts: 6756
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Run multi-line Python script within AppleScript?

I don't think there's a man page for indigo-host.

Code: Select all
Dave@my-mac ~ % man indigo-host
No manual entry for indigo-host

I believe all the helper apps are in usr/local/bin.

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Mon Aug 15, 2022 12:15 pm
FlyingDiver offline
User avatar
Posts: 7221
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Run multi-line Python script within AppleScript?

billearl wrote:
How do I get a man page for indigo-host? I'd like to see the options available. Thanks.
It’s a Python script. I just read the code.


Sent from my iPhone using Tapatalk

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Who is online

Users browsing this forum: No registered users and 4 guests