Page 1 of 1

Applescript to Python

PostPosted: Sun Jun 11, 2017 3:38 pm
by CraigM
I'm on a mission to convert all my Applescripts to Python. Could someone please show me how the following would read in Python.

Code: Select all
set value of variable "Month" to (month of (current date)) as text

Code: Select all
set value of variable "DayofWeek" to (weekday of (current date)) as text

These are being used as Conditions in Schedules
Thanks

Re: Applescript to Python

PostPosted: Sun Jun 11, 2017 4:31 pm
by kwijibo007
I'm assuming you want the variables to be the name of the day e.g 'Monday' and the name of the month e.g June.

Code: Select all
import datetime
now = datetime.datetime.now()

Month = now.strftime('%B')
DayofWeek = now.strftime('%A')



Good luck pythoning!







Sent from my iPad using Tapatalk

Re: Applescript to Python

PostPosted: Sun Jun 11, 2017 7:23 pm
by DaveL17
kwijibo007 wrote:
I'm assuming you want the variables to be the name of the day e.g 'Monday' and the name of the month e.g June.

Code: Select all
import datetime
now = datetime.datetime.now()

Month = now.strftime('%B')
DayofWeek = now.strftime('%A')




Two more lines are needed to update the variables.

Code: Select all
import datetime
now = datetime.datetime.now()

Month = now.strftime('%B')
DayofWeek = now.strftime('%A')

indigo.variable.updateValue(314742858, Month)
indigo.variable.updateValue(1928699259, DayofWeek)

You'll need to change the numbers to the IDs of your variables.

Re: Applescript to Python

PostPosted: Mon Jun 12, 2017 2:17 pm
by CraigM
Almost there, but not exactly what I was looking for as I have two different schedules and this code seems to have combined them both into one.

How can I split this so that it resembles the two separate lines of code in the original post?

Re: Applescript to Python

PostPosted: Mon Jun 12, 2017 6:07 pm
by DaveL17
Is this what you're looking for?

Code: Select all
import datetime
now = datetime.datetime.now()

DayofWeek = now.strftime('%A')
indigo.variable.updateValue(314742858, DayofWeek)

Code: Select all
import datetime
now = datetime.datetime.now()

Month = now.strftime('%B')
indigo.variable.updateValue(314742858, Month)


ETA - forgot the variable update bit.

Re: Applescript to Python

PostPosted: Mon Jun 12, 2017 9:41 pm
by CraigM
Thanks to everyone for getting this to work BUT:

Can someone explain why the Applescript code went in the Schedule/Condition/if script returns true screen:
Code: Select all
set value of variable "DayofWeek" to (weekday of (current date)) as text

And the Python code goes in as a Server Action
Code: Select all
import datetime
now = datetime.datetime.now()

DayofWeek = now.strftime('%A')
indigo.variable.updateValue(123456789, DayofWeek)

Re: Applescript to Python

PostPosted: Tue Jun 13, 2017 3:42 am
by DaveL17
I think Matt or Jay will have to answer the why. I haven't really dabbled too much with AppleScript, and forget that feature is even there. I find that I'm able to do everything I need using either Indigo's native controls or with Python (or both).

Re: Applescript to Python

PostPosted: Tue Jun 13, 2017 9:37 am
by jay (support)
CraigM wrote:
Thanks to everyone for getting this to work BUT:

Can someone explain why the Applescript code went in the Schedule/Condition/if script returns true screen:
Code: Select all
set value of variable "DayofWeek" to (weekday of (current date)) as text

And the Python code goes in as a Server Action
Code: Select all
import datetime
now = datetime.datetime.now()

DayofWeek = now.strftime('%A')
indigo.variable.updateValue(123456789, DayofWeek)


Well - the AppleScript you have above doesn't return True or False, so it really didn't belong in the Condition script box but rather would be more appropriate as a Server Action. The purpose of the Condition Script is to return either a True or False value that would then either allow the actions connected to the schedule to run or not based on the logic. Setting the value there is somewhat inappropriate since that's really not it's purpose.

The result, however, is basically the same.

Re: Applescript to Python

PostPosted: Sun Mar 10, 2019 12:41 pm
by indigobo
Hi All!

I know this is an old topic, but thought I'd ask anyway... Using python, what if I wanted the day name of tomorrow? In other words if today is Sunday then day+1 = Monday, day +2 = Tuesday, etc.

Code: Select all
import datetime
tomorrowWeekday = indigo.server.getTime().date() + datetime.timedelta(days=1)
indigo.variable.updateValue(421598678, str(tomorrowWeekday))


This gives me the date of tomorrow but not the day name.

Re: Applescript to Python

PostPosted: Sun Mar 10, 2019 3:13 pm
by jay (support)
Code: Select all
 tomorrowWeekday.strftime("%A")