Applescript to Python

Posted on
Sat Sep 29, 2018 3:28 pm
PME999 offline
Posts: 54
Joined: Jul 28, 2012

Applescript to Python

Hello,
I need to change my applescript to Python before next version. I don't know Python, so I need help. Here below some of my applescripts. I would appreciate to have the translation of these example.
thanks a lot
Patrice

SCRIPT 1:
Code: Select all
tell application "IndigoServer"
   set VAR to value of variable "STATUTPORTAILOUVERT" as string
   set theURL to "http://192.168.0.49/~patriceagnesmetairie1/logger7.php?" & "f=STATUTPORTAILOUVERT&v=" & VAR
   do shell script "curl " & quoted form of theURL
end tell


SCRIPT 2:
Code: Select all
tell application "IndigoServer"
   -- calcul du vent total de la journée
   set VITVENT to (value of variable "OREGONVITVENT") as real
   set VENTOT to (value of variable "OREGONVENTTOT") as real
   set VENTTOT to (VITVENT) / 100 + VENTOT
   set value of variable "OREGONVENTTOT" to VENTTOT
end tell


SCRIPT 3:
Code: Select all
tell application "IndigoServer"
   set URLChromeResponse to do shell script "curl http://127.0.0.1/~patriceagnesmetairie1/FLIPRPH.php"
   set VAR11 to URLChromeResponse
   set URLChromeResponse to do shell script "curl http://127.0.0.1/~patriceagnesmetairie1/FLIPROXYDOREDUCTION.php"
   set VAR22 to URLChromeResponse
   set URLChromeResponse to do shell script "curl http://127.0.0.1/~patriceagnesmetairie1/FLIPRBATTERYLEVEL.php"
   set VAR44 to URLChromeResponse
   set value of variable "FLIPRPH" to VAR11
   set value of variable "FLIPROXYDOREDUCTION" to VAR22
   set value of variable "FLIPRBATTERYLEVEL" to VAR44
end tell


SCRIPT 4 :
Code: Select all
set theURL to "http://192.168.0.49/~patriceagnesmetairie1/DOMOMETEOdelete5jours.php"
do shell script "curl " & quoted form of theURL

SCRIPT 5 :
Code: Select all
tell application "IndigoServer"
   set TEMPOREGONEXT to (value of variable "ZWAVETEMPOREGONEXTERIEURAVANTCORRECTION") as real
   set TEMPOREGONEXT to TEMPOREGONEXT - 0
   set value of variable "ZWAVETEMPOREGONEXTERIEUR" to TEMPOREGONEXT
   
   set VARPISSANS to (value of variable "ZWAVETEMPPISCSANSCORRECT") as real
   set VARPIS2 to (value of variable "ZWAVETEMPPIS2") as real
   if VARPISSANS < VARPIS2 * 2 and VARPISSANS > VARPIS2 * 0.5 then
      set value of variable "ZWAVETEMPPIS2" to VARPISSANS
   end if
   set VARTHEOSANS to (value of variable "ZWAVETEMPTHEOSANSCORRECT") as real
   set VARTHEO to (value of variable "ZWAVETEMPTHEO") as real
   if VARTHEOSANS < VARTHEO * 1.2 and VARTHEOSANS > VARTHEO * 0.8 then
      set value of variable "ZWAVETEMPTHEO" to VARTHEOSANS
   end if
   set VARCELIASANS to (value of variable "ZWAVETEMPCELIASANSCORRECT") as real
   set VARCELIA to (value of variable "ZWAVETEMPCELIA") as real
   if VARCELIASANS < VARCELIA * 1.2 and VARCELIASANS > VARCELIA * 0.8 then
      set value of variable "ZWAVETEMPCELIA" to VARCELIASANS
   end if
end tell

SCRIPT 6 :
Code: Select all
tell application "IndigoServer"
   set VAR111 to (value of variable "ZWAVECONSIGNETHERMOSTATSMARTPAC") as real
   set VAR222 to VAR111 + 0.1
   set value of variable "ZWAVECONSIGNETHERMOSTATSMARTPAC" to VAR222
end tell


SCRIPT 7 :
Code: Select all
tell application "IndigoServer"
   set URLChromeResponse to do shell script "curl http://127.0.0.1/~patriceagnesmetairie1/ACTIVERMODEALARMEAWAY.php"
end tell

Posted on
Sat Sep 29, 2018 5:33 pm
jay (support) offline
Site Admin
User avatar
Posts: 18199
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Applescript to Python

[MODERATOR NOTE] moved to a new topic - please post conversion requests in separate topics.

I'll do conversions below, but in the future it would be helpful to put one script per topic so it's easier for others to jump in.

In all of the conversions below, you should substitute the ID of the variable for IDOF_*. I haven't tested all of them, so it's possible I've made some typos, but they should be close and easily fixed if there are issues.

PME999 wrote:
SCRIPT 1:
Code: Select all
tell application "IndigoServer"
   set VAR to value of variable "STATUTPORTAILOUVERT" as string
   set theURL to "http://192.168.0.49/~patriceagnesmetairie1/logger7.php?" & "f=STATUTPORTAILOUVERT&v=" & VAR
   do shell script "curl " & quoted form of theURL
end tell



Here's an approximate conversion. Note that you could still call curl, but Python can natively open URLs:

Code: Select all
import requests
var_value = indigo.variables[IDOF_STATUTPORTAILOUVERT].value  # insert the ID of the 'STATUTPORTAILOUVERT' variable
the_url = "http://192.168.0.49/~patriceagnesmetairie1/logger7.php?f=STATUTPORTAILOUVERT&v={}".format(var_value)
requests.get(the_url)


PME999 wrote:
SCRIPT 2:
Code: Select all
tell application "IndigoServer"
   -- calcul du vent total de la journée
   set VITVENT to (value of variable "OREGONVITVENT") as real
   set VENTOT to (value of variable "OREGONVENTTOT") as real
   set VENTTOT to (VITVENT) / 100 + VENTOT
   set value of variable "OREGONVENTTOT" to VENTTOT
end tell



Code: Select all
vitvent = indigo.variables[IDOF_OREGONVITVENT].getValue(float)
ventot = indigo.variables[IDOF_OREGONVENTTOT].getValue(float)
venttot = vitvent / 100 + ventot
indigo.variable.updateValue(IDOF_OREGONVENTTOT, value=str(venttot))


PME999 wrote:
SCRIPT 3:
Code: Select all
tell application "IndigoServer"
   set URLChromeResponse to do shell script "curl http://127.0.0.1/~patriceagnesmetairie1/FLIPRPH.php"
   set VAR11 to URLChromeResponse
   set URLChromeResponse to do shell script "curl http://127.0.0.1/~patriceagnesmetairie1/FLIPROXYDOREDUCTION.php"
   set VAR22 to URLChromeResponse
   set URLChromeResponse to do shell script "curl http://127.0.0.1/~patriceagnesmetairie1/FLIPRBATTERYLEVEL.php"
   set VAR44 to URLChromeResponse
   set value of variable "FLIPRPH" to VAR11
   set value of variable "FLIPROXYDOREDUCTION" to VAR22
   set value of variable "FLIPRBATTERYLEVEL" to VAR44
end tell



Code: Select all
import requests
var11 = requests.get("http://127.0.0.1/~patriceagnesmetairie1/FLIPRPH.php")
var22 = requests.get("http://127.0.0.1/~patriceagnesmetairie1/FLIPROXYDOREDUCTION.php")
var44 = requests.get("http://127.0.0.1/~patriceagnesmetairie1/FLIPRBATTERYLEVEL.php")
indigo.variable.updateValue(IDOF_FLIPRPH, value=str(var11))
indigo.variable.updateValue(IDOF_FLIPROXYDOREDUCTION, value=str(var22))
indigo.variable.updateValue(IDOF_FLIPRBATTERYLEVEL, value=str(var44))


PME999 wrote:
SCRIPT 4 :
Code: Select all
set theURL to "http://192.168.0.49/~patriceagnesmetairie1/DOMOMETEOdelete5jours.php"
do shell script "curl " & quoted form of theURL



Technically, this one would be OK to remain in AppleScript as long as it was run as an external script. However, the Python version is super simple:

Code: Select all
import requests
requests.get("http://192.168.0.49/~patriceagnesmetairie1/DOMOMETEOdelete5jours.php")


PME999 wrote:
SCRIPT 5 :
Code: Select all
tell application "IndigoServer"
   set TEMPOREGONEXT to (value of variable "ZWAVETEMPOREGONEXTERIEURAVANTCORRECTION") as real
   set TEMPOREGONEXT to TEMPOREGONEXT - 0
   set value of variable "ZWAVETEMPOREGONEXTERIEUR" to TEMPOREGONEXT
   
   set VARPISSANS to (value of variable "ZWAVETEMPPISCSANSCORRECT") as real
   set VARPIS2 to (value of variable "ZWAVETEMPPIS2") as real
   if VARPISSANS < VARPIS2 * 2 and VARPISSANS > VARPIS2 * 0.5 then
      set value of variable "ZWAVETEMPPIS2" to VARPISSANS
   end if
   set VARTHEOSANS to (value of variable "ZWAVETEMPTHEOSANSCORRECT") as real
   set VARTHEO to (value of variable "ZWAVETEMPTHEO") as real
   if VARTHEOSANS < VARTHEO * 1.2 and VARTHEOSANS > VARTHEO * 0.8 then
      set value of variable "ZWAVETEMPTHEO" to VARTHEOSANS
   end if
   set VARCELIASANS to (value of variable "ZWAVETEMPCELIASANSCORRECT") as real
   set VARCELIA to (value of variable "ZWAVETEMPCELIA") as real
   if VARCELIASANS < VARCELIA * 1.2 and VARCELIASANS > VARCELIA * 0.8 then
      set value of variable "ZWAVETEMPCELIA" to VARCELIASANS
   end if
end tell



Code: Select all
temporegonext = indigo.variables[IDOF_ZWAVETEMPOREGONEXTERIEURAVANTCORRECTION].getValue(float)
indigo.variable.updateValue(IDOF_ZWAVETEMPOREGONEXTERIEUR, value=str(temporegonext - 0))

varpissans = indigo.variables[IDOF_ZWAVETEMPPISCSANSCORRECT].getValue(float)
varpis2 = indigo.variables[IDOF_ZWAVETEMPPIS2].getValue(float)
if varpissans < (varpis2 * 2) and varpissans > (varpis2 * 0.5):
    indigo.variable.updateValue(IDOF_ZWAVETEMPPIS2, value=str(varpissans))

vartheosans = indigo.variables[IDOF_ZWAVETEMPTHEOSANSCORRECT].getValue(float)
vartheo = indigo.variables[IDOF_ZWAVETEMPTHEO].getValue(float)
if vartheosans < (vartheo * 1.2) and vartheosans > (vartheo * 0.8):
    indigo.variable.updateValue(IDOF_ZWAVETEMPTHEO, value=str(vartheosans))

varceliasans = indigo.variables[IDOF_ZWAVETEMPCELIASANSCORRECT].getValue(float)
varcelia = indigo.variables[IDOF_ZWAVETEMPCELIA].getValue(float)
if varceliasans < (varcelia * 1.2) and varceliasans > (varcelia * 0.8):
    indigo.variable.updateValue(IDOF_ZWAVETEMPCELIA, value=str(varceliasans))


PME999 wrote:
SCRIPT 6 :
Code: Select all
tell application "IndigoServer"
   set VAR111 to (value of variable "ZWAVECONSIGNETHERMOSTATSMARTPAC") as real
   set VAR222 to VAR111 + 0.1
   set value of variable "ZWAVECONSIGNETHERMOSTATSMARTPAC" to VAR222
end tell



Code: Select all
var111 = indigo.variables[IDOF_ZWAVECONSIGNETHERMOSTATSMARTPAC].getValue(float)
indigo.variable.updateValue(IDOF_ZWAVECONSIGNETHERMOSTATSMARTPAC, value=str(var111 + 0.1))


PME999 wrote:
SCRIPT 7 :
Code: Select all
tell application "IndigoServer"
   set URLChromeResponse to do shell script "curl http://127.0.0.1/~patriceagnesmetairie1/ACTIVERMODEALARMEAWAY.php"
end tell



Code: Select all
import requests
url_chrome_response = requests.get("http://127.0.0.1/~patriceagnesmetairie1/ACTIVERMODEALARMEAWAY.php")

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Sun Sep 30, 2018 1:25 am
PME999 offline
Posts: 54
Joined: Jul 28, 2012

Re: Applescript to Python

Thanks a lot Jay for your quick answer and noted for the topic. I will do so next time.
Sorry for the question, but I tried the Python version and I have errors :

Script Error embedded script: global name 'IDOF_STATUTPORTAILOUVERT' is not defined
Script Error Exception Traceback (most recent call shown last):

embedded script, line 2, at top level
NameError: global name 'IDOF_STATUTPORTAILOUVERT' is not defined

I have the same error for each variable not defined.
I do probably a mistake but I'm at the level 0 for Python !
Thanks
Patrice

Posted on
Sun Sep 30, 2018 1:45 am
autolog offline
Posts: 3988
Joined: Sep 10, 2013
Location: West Sussex, UK [GMT aka UTC]

Re: Applescript to Python

As Jay said in his reply:
... In all of the conversions below, you should substitute the ID of the variable for IDOF_*. ...

So you need to look at your variables in the Indigo Variable List Window and then for each variable substitute its ID into the Python script.
So where you have IDOF_STATUTPORTAILOUVERT in your script, that should be replaced with the variable's ID e.g. 12345678.

In the Indigo Variable List window, you can select the required variable, right click and click the Copy ID menu entry . This will copy the ID which you can then paste into your Python script.

Hope this helps. :)

Posted on
Sun Sep 30, 2018 2:00 am
PME999 offline
Posts: 54
Joined: Jul 28, 2012

Re: Applescript to Python

Great, many thanks. My habit was with applescript to use the name of the variable but not the ID !
thanks again
Patrice

Posted on
Sun Sep 30, 2018 3:11 am
PME999 offline
Posts: 54
Joined: Jul 28, 2012

Re: Applescript to Python

Hello,
I still have a problem with the script :

Python script :
import requests
var11 = requests.get("http://127.0.0.1/~patriceagnesmetairie1/MODEALARME.php")
indigo.variable.updateValue(50553206, value=str(var11))

the PHP file is :
<?php
$url = "http://192.168.0.86:3480/data_request?id=variableget&Variable=Mode";
$contenu = file_get_contents($url);
echo $contenu;
?>

the IP adress is a VERA controller where I'm requesting the value of the Mode variable. all is working fine with the applescript below, but with python, the value coming back in my variable is <Response [200]>

applescript :
tell application "IndigoServer"
set URLChromeResponse to do shell script "curl http://127.0.0.1/~patriceagnesmetairie1/MODEALARME.php"
set VAR11 to URLChromeResponse
set value of variable "MODEALARME" to VAR11
end tell

Thanks for help
Patrice

Posted on
Sun Sep 30, 2018 7:40 am
PME999 offline
Posts: 54
Joined: Jul 28, 2012

Re: Applescript to Python

I have an other problem with syntax of and url. The applescript version is

Code: Select all
set ANNEE to value of variable "STATUTANNEE" as string
   set MOIS to value of variable "STATUTNUMEROMOIS" as string
   set CONSOCUMUL to value of variable "ZZCONSOANNEEOK" as string
   set theURL1 to "http://192.168.0.49/~patriceagnesmetairie1/logger9.php?&f='CUMUL" & ANNEE & "'&m='" & MOIS & "'&v='" & CONSOCUMUL & "'"
   do shell script "curl " & quoted form of theURL1


Code: Select all
I tried the python syntax :
import requests
ANNEE = indigo.variables[882143320].value
MOIS = indigo.variables[529934040].value
CONSOCUMUL = indigo.variables[1982363532].value
the_url = "http://192.168.0.49/~patriceagnesmetairie1/logger9.php?f= CUMUL{}".format(ANNEE) + "&m={}".format(MOIS) + "&v={}".format(CONSOCUMUL)
requests.get(the_url)


but it's not working. Can someday help me on what is wrong ?
Thanks
Patrice

Posted on
Sun Sep 30, 2018 9:18 am
kw123 offline
User avatar
Posts: 8333
Joined: May 12, 2013
Location: Dallas, TX

Re: Applescript to Python

Code: Select all
the_url = "http://192.168.0.49/~patriceagnesmetairie1/logger9.php?f='CUMUL" +ANNEE+ "'&m='" +MOIS + "'&v='"+CONSOCUMUL+"'"
should do it..

As ANNEE etc are already strings no need to format them.. and added the missing '

Karl

Posted on
Sun Sep 30, 2018 9:37 am
PME999 offline
Posts: 54
Joined: Jul 28, 2012

Re: Applescript to Python

Thanks a lot Karl for your help. It's working now.
If I may, do you have an idea for the post just before I did concerning the request with php file to my VERA controller
big thanks
Patrice

Posted on
Sun Sep 30, 2018 12:16 pm
kw123 offline
User avatar
Posts: 8333
Joined: May 12, 2013
Location: Dallas, TX

Re: Applescript to Python

Code: Select all
import subprocess
abc = subprocess.Popen("/usr/bin/curl http://127.0.0.1/~patriceagnesmetairie1/MODEALARME.php", stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True).communicate()
indigo.variable.updateValue("MODEALARME", value= abc[0])

this will launch a shell like the "do shell script ..." and will return the output of the curl command

abc contains the std out in [0] as well as std error in [1]

you could also do the "request"way .

Karl

you MUST use the full path to curl as the shell does not know any PATH setting when it is opened this way

Posted on
Sun Sep 30, 2018 12:23 pm
jay (support) offline
Site Admin
User avatar
Posts: 18199
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Applescript to Python

PME999 wrote:
import requests
var11 = requests.get("http://127.0.0.1/~patriceagnesmetairie1/MODEALARME.php")
indigo.variable.updateValue(50553206, value=str(var11))


change the last line to:

Code: Select all
indigo.variable.updateValue(50553206, value=str(var11.contents))


The return value from the requests.get call is a reply object. You want the contents of the reply. If that doesn't work then try var11.text.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Sun Sep 30, 2018 12:55 pm
PME999 offline
Posts: 54
Joined: Jul 28, 2012

Re: Applescript to Python

Many thanks Karl and Jay
It’s working now
Is it possible to see the name of a variable in Python instead of the ID which is more difficult to read and understand ?
Thanks
Patrice

Posted on
Sun Sep 30, 2018 12:57 pm
kw123 offline
User avatar
Posts: 8333
Joined: May 12, 2013
Location: Dallas, TX

Re: Applescript to Python

What do you mean by “see”

Use?


Sent from my iPhone using Tapatalk

Posted on
Sun Sep 30, 2018 1:25 pm
jay (support) offline
Site Admin
User avatar
Posts: 18199
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Applescript to Python

PME999 wrote:
Many thanks Karl and Jay
It’s working now
Is it possible to see the name of a variable in Python instead of the ID which is more difficult to read and understand ?
Thanks
Patrice


You can use the name in place of the ID, but it is highly discouraged since a variable name change will break the script. You can add a comment in the script to remind you what the variables are for, though of course the script itself is probably the best documentation. Comments are definitely your friend.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Sun Sep 30, 2018 5:21 pm
kw123 offline
User avatar
Posts: 8333
Joined: May 12, 2013
Location: Dallas, TX

Re: Applescript to Python

in principle I agree with Jay , but in some circumstances it is better too use the name

name: use "theVariableName" in quotes
id: use 12345 no quotes

I recycle some of the variables in some of my plugins to save space in the logger database.
Deleting and recreating the variable removes the tables (in postgres) and starts from scratch and then they have a new ID

Karl

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 2 guests