Page 2 of 2

Re: Run multi-line Python script within AppleScript?

PostPosted: Mon Aug 15, 2022 6:39 pm
by ryanbuckner
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)

Re: Run multi-line Python script within AppleScript?

PostPosted: Mon Aug 15, 2022 7:00 pm
by FlyingDiver
Yeah, but how does he do THAT from AS?

Re: Run multi-line Python script within AppleScript?

PostPosted: Mon Aug 15, 2022 8:29 pm
by jay (support)
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).

Re: Run multi-line Python script within AppleScript?

PostPosted: Mon Aug 15, 2022 9:48 pm
by billearl
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.

Re: Run multi-line Python script within AppleScript?

PostPosted: Tue Aug 16, 2022 12:00 pm
by billearl
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)

Re: Run multi-line Python script within AppleScript?

PostPosted: Tue Aug 16, 2022 2:00 pm
by billearl
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\")'"

Re: Run multi-line Python script within AppleScript?

PostPosted: Tue Aug 16, 2022 3:33 pm
by FlyingDiver
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.

Re: Run multi-line Python script within AppleScript?

PostPosted: Tue Aug 16, 2022 4:15 pm
by billearl
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.