Applescript to Python

Posted on
Sun Jun 11, 2017 3:38 pm
CraigM offline
Posts: 578
Joined: Oct 28, 2007

Applescript to Python

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

Posted on
Sun Jun 11, 2017 4:31 pm
kwijibo007 offline
Posts: 325
Joined: Sep 27, 2013
Location: Melbourne, Australia

Re: Applescript to Python

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

Posted on
Sun Jun 11, 2017 7:23 pm
DaveL17 offline
User avatar
Posts: 6744
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Applescript to Python

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.

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

[My Plugins] - [My Forums]

Posted on
Mon Jun 12, 2017 2:17 pm
CraigM offline
Posts: 578
Joined: Oct 28, 2007

Re: Applescript to Python

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?

Posted on
Mon Jun 12, 2017 6:07 pm
DaveL17 offline
User avatar
Posts: 6744
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Applescript to Python

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.

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

[My Plugins] - [My Forums]

Posted on
Mon Jun 12, 2017 9:41 pm
CraigM offline
Posts: 578
Joined: Oct 28, 2007

Re: Applescript to Python

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)

Posted on
Tue Jun 13, 2017 3:42 am
DaveL17 offline
User avatar
Posts: 6744
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Applescript to Python

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).

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

[My Plugins] - [My Forums]

Posted on
Tue Jun 13, 2017 9:37 am
jay (support) offline
Site Admin
User avatar
Posts: 18200
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Applescript to Python

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.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Sun Mar 10, 2019 12:41 pm
indigobo offline
User avatar
Posts: 22
Joined: Jan 04, 2019
Location: Raleigh, NC

Re: Applescript to Python

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.

Posted on
Sun Mar 10, 2019 3:13 pm
jay (support) offline
Site Admin
User avatar
Posts: 18200
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Applescript to Python

Code: Select all
 tomorrowWeekday.strftime("%A")

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 4 guests