Run multi-line Python script within AppleScript?

Posted on
Mon Aug 15, 2022 6:39 pm
ryanbuckner offline
Posts: 1080
Joined: Oct 08, 2011
Location: Northern Virginia

Re: Run multi-line Python script within AppleScript?

FlyingDiver wrote:
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.


One "convoluted" way is to put the device ID into a variable first and then run the script.

Code: Select all
import sys

#replace the id of your own variable below
myDevID = int(indigo.variables[12345678].value)
devIsOn = indigo.devices[myDevID].states['onOffState']
if (devIsOn):
   indigo.device.turnOff(myDevID)
else:
   indigo.device.turnOn(myDevID)

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

Re: Run multi-line Python script within AppleScript?

Yeah, but how does he do THAT from AS?

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

Posted on
Mon Aug 15, 2022 8:29 pm
jay (support) offline
Site Admin
User avatar
Posts: 18212
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Run multi-line Python script within AppleScript?

I'd recommend using the REST API.

And if you're open to alternative tool suggestions, Keyboard Maestro is (IMO) a much more capable product than Butler. I've been using it for many years and it has a lot more capabilities (based on what I read on the Butler website).

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Mon Aug 15, 2022 9:48 pm
billearl offline
Posts: 26
Joined: Aug 16, 2005

Re: Run multi-line Python script within AppleScript?

Thanks one and all. For now at least, I'll just use Butler to run AppleScripts such as,
Code: Select all
do shell script "/usr/local/bin/indigo-host -x '/Users/billearl/Documents/Scripts/Indigo Python Scripts/Fan.py'"
so, a separate ".py" file for each device to be toggled (using its onOffState in a conditional). That will satisfy my simple needs.

I use Keyboard Maestro too, but haven't kept up with its newer features. KM has become much more powerful, and complex, in recent years.

Other solutions presented here, such as REST API, are a bit beyond my current interest level. Nowadays, I'm satisfied to know just enough to get things to reliably work the way I want, whether it be AppleScript, Python, Keyboard Maestro, VBA, Arduino, ladder logic, or whatever.

Thanks again for your generous and valuable support.

Posted on
Tue Aug 16, 2022 12:00 pm
billearl offline
Posts: 26
Joined: Aug 16, 2005

Re: Run multi-line Python script within AppleScript?

Not quite done yet.
There is one on/off device that I sometimes use Indigo, and sometimes use an Insteon Mini Remote, to control. So I want to do a status request when using Indigo. Why does this script not work as expected?
Code: Select all
devName = "Fan"
indigo.device.statusRequest(devName)
#time.sleep(1)
devIsOn = indigo.devices[devName].states['onOffState']
if (devIsOn):
   indigo.device.turnOff(devName)
else:
   indigo.device.turnOn(devName)
Enabling time.sleep(1) simply stops the script, which I also don't understand.

If I run two consecutive scripts, it does work as expected.
Code: Select all
devName = "Fan"
indigo.device.statusRequest(devName)
Code: Select all
devName = "Fan"
devIsOn = indigo.devices[devName].states['onOffState']
if (devIsOn):
   indigo.device.turnOff(devName)
else:
   indigo.device.turnOn(devName)

Posted on
Tue Aug 16, 2022 2:00 pm
billearl offline
Posts: 26
Joined: Aug 16, 2005

Re: Run multi-line Python script within AppleScript?

I'd be interested in an answer for my post just above, but the following AppleScript works fine. It's actually what I was using before creating this thread, but with the addition of an indigo.device.statusRequest().
Code: Select all
do shell script "/usr/local/bin/indigo-host -e 'indigo.device.statusRequest(\"Fan\")'"
do shell script "/usr/local/bin/indigo-host -e 'indigo.device.toggle(\"Fan\")'"

Posted on
Tue Aug 16, 2022 3:33 pm
FlyingDiver offline
User avatar
Posts: 7210
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Run multi-line Python script within AppleScript?

The time.sleep() call might be failing because I don't see an import for the time module.

Otherwise, I expect the script is hitting the test section before the status request has completed, so you're not seeing the updated value. indigo.device.statusRequest() is asynchronous, it does not wait to return.

If you can get the sleep call to work, then you'll probably get the updated value.

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

Posted on
Tue Aug 16, 2022 4:15 pm
billearl offline
Posts: 26
Joined: Aug 16, 2005

Re: Run multi-line Python script within AppleScript?

Yes, that worked.

Code: Select all
import time

devName = "Fan"
indigo.device.statusRequest(devName)
time.sleep(1)
devIsOn = indigo.devices[devName].states['onOffState']
if (devIsOn):
   indigo.device.turnOff(devName)
else:
   indigo.device.turnOn(devName)
Thanks again.

Who is online

Users browsing this forum: No registered users and 2 guests