Help converting old iCal script to python

Posted on
Sun Nov 08, 2020 1:10 pm
digtrail offline
Posts: 28
Joined: Jun 02, 2016

Help converting old iCal script to python

I have an old AppleScript that I used to check my iCal for my work schedule using icalbuddy. The script then modifies four different variables based on what is returned. I put this together ages ago from some old examples I had seen here on the forum. I found one or two new python scripts that I might be able to modify but they are much more involved than this old script is. Problem is, I cant figure out how to put this in Python as my coding skills are about 1-2 levels above copy paste at this time of my life. Any help would be greatly appreciated. Thanks.

Code: Select all
 set LaString to (do shell script "/usr/local/bin/icalbuddy -nc -ic Work eventsToday")
 if LaString contains "Day Work" then
    set value of variable "DayWork" to "true"
 else
    set value of variable "DayWork" to "false"
 end if
 
 if LaString contains "Night Work" then
    set value of variable "NightWork" to "true"
 else
    set value of variable "NightWork" to "false"
 end if
 
 if LaString contains "OFF" then
    set value of variable "Off" to "true"
 else
    set value of variable "Off" to "false"
 end if
 
 if LaString contains "VAC" then
    set value of variable "Vac" to "true"
 else
    set value of variable "Vac" to "false"
 end if
 

Posted on
Mon Nov 09, 2020 10:52 am
jay (support) offline
Site Admin
User avatar
Posts: 18199
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Help converting old iCal script to python

I have no idea what icalbuddy is, but just a straight port of the script would be something like this (untested of course):

Code: Select all
# Substitute anything below that starts with IDOF_ with the actual Indigo ID of the
# specified variable - right click the variable in the Variable List window and select Copy ID
import os
la_string = os.popen('/usr/local/bin/icalbuddy -nc -ic Work eventsToday').read()
if "DayWork" in la_string:
   indigo.variables.updateValue(IDOF_DayWork, value='true')
else:
   indigo.variables.updateValue(IDOF_DayWork, value='false')

if "Night Work" in la_string:
   indigo.variables.updateValue(IDOF_NightWork, value='true')
else:
   indigo.variables.updateValue(IDOF_NightWork, value='false')

if "OFF" in la_string:
   indigo.variables.updateValue(IDOF_Off, value='true')
else:
   indigo.variables.updateValue(IDOF_Off, value='false')

if "VAC" in la_string:
   indigo.variables.updateValue(IDOF_Vac, value='true')
else:
   indigo.variables.updateValue(IDOF_Vac, value='false')

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Mon Nov 09, 2020 11:22 am
digtrail offline
Posts: 28
Joined: Jun 02, 2016

Re: Help converting old iCal script to python

Thanks for that. icallbuddy is a tiny little app that I found through the forum here. It will look for events on iCal and then can be imported into Indigo. Worked great until I moved to 7.4 awhile back. Didn't need this script at that time any more, but now I do again.

I updated variable id's and tried a run. I got this return:

Script Error embedded script: 'VariableList' object has no attribute 'updateValue'
Script Error Exception Traceback (most recent call shown last):

embedded script, line 8, at top level
AttributeError: 'VariableList' object has no attribute 'updateValue'

Code: Select all
# Substitute anything below that starts with IDOF_ with the actual Indigo ID of the
# specified variable - right click the variable in the Variable List window and select Copy ID
import os
la_string = os.popen('/usr/local/bin/icalbuddy -nc -ic Work eventsToday')
if "DayWork" in la_string:
   indigo.variables.updateValue(1422763041, value='true')
else:
   indigo.variables.updateValue(1422763041, value='false')

if "Night Work" in la_string:
   indigo.variables.updateValue(15646541, value='true')
else:
   indigo.variables.updateValue(15646541, value='false')

if "OFF" in la_string:
   indigo.variables.updateValue(790632292, value='true')
else:
   indigo.variables.updateValue(790632292, value='false')

if "VAC" in la_string:
   indigo.variables.updateValue(41667262, value='true')
else:
   indigo.variables.updateValue(41667262, value='false')

Posted on
Mon Nov 09, 2020 11:59 am
howartp offline
Posts: 4559
Joined: Jan 09, 2014
Location: West Yorkshire, UK

Re: Help converting old iCal script to python

As a guess, but I’m not at my PC:

Change indigo.variables.updateValue() to indigo.variable.updateValue()

ie make variables singular.


Sent from my iPhone using Tapatalk Pro

Posted on
Mon Nov 09, 2020 12:41 pm
jay (support) offline
Site Admin
User avatar
Posts: 18199
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Help converting old iCal script to python

Yep, I think that's it. Stumbled over my own cleverness (I proposed the variables vs variable naming scheme, which I've come to regret somewhat).

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Mon Nov 09, 2020 3:56 pm
digtrail offline
Posts: 28
Joined: Jun 02, 2016

Re: Help converting old iCal script to python

It runs now. BUT it doesn't do anything. So I tried to run icalbuddy direct in terminal and get this in return: bad CPU type in executable: /Users/Kevin/Downloads/icalBuddy-v1.8.8/icalBuddy

My guess is it is now longer compatible. It hasn't been updated for a long time.

Any other ideas on how to parse out daily calendar events ahead of time? I tried playing with cynical calendars but can't seem to figure hour to get it to see the event and modify the variable before the even actually happens. My old script ran at 1201a to look ahead in the day so that the variables would be changed and then other schedules ran later in the day at different times based on that. I searched and there are very few calendar threads here on the forum.

Thanks for the help

Posted on
Tue Nov 17, 2020 4:39 pm
digtrail offline
Posts: 28
Joined: Jun 02, 2016

Re: Help converting old iCal script to python

Nearing success with this. I figured that the bad cpu type was bc I haven't used icalbuddy since before updating to os 10.15 and that it was a 32bit compiled executable. That assumption was right and even tho the original developer hasn't updated it in about 4-5yrs I was able to find an update that works all the way thru 10.17 now by following pull requests on git. Updated version of icalbuddy can be found here: https://github.com/DavidKaluta/icalBuddy64/releases

Installed updated version of icalbuddy and it runs successfully in terminal. However it seems that the script is able to "see" the output as it has in the past bc is it changes all of my variables to false when run in Indigo instead of recognizing that for instance today I am OFF. Any ideas on this? I appreciate the help to this point. Below is output in terminal when run successfully there

Code: Select all
Kevin@unifi ~ % icalbuddy -ic Work eventsToday
• Night Work (Work)
    notes: T-2
    ... - 7:00 AM
• Send mask email  (Work)
    6:45 AM - 6:50 AM
• OFF (Work)
    7:00 AM - 11:59 PM

Posted on
Wed Nov 18, 2020 9:32 am
jay (support) offline
Site Admin
User avatar
Posts: 18199
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Help converting old iCal script to python

Sorry, left off something important - change this line:

Code: Select all
la_string = os.popen('/usr/local/bin/icalbuddy -nc -ic Work eventsToday')


to this:

Code: Select all
la_string = os.popen('/usr/local/bin/icalbuddy -nc -ic Work eventsToday').read()


(I also changed it in my post further up to avoid confusion).

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Wed Nov 18, 2020 4:36 pm
digtrail offline
Posts: 28
Joined: Jun 02, 2016

Re: Help converting old iCal script to python

That worked. I'm back to automating based on my work schedule. Thanks!

Posted on
Mon Nov 14, 2022 9:17 am
digtrail offline
Posts: 28
Joined: Jun 02, 2016

Re: Help converting old iCal script to python

Hello all,

Having a new issue with this one. Realized certain schedules didn't seem to be working as should so I checked my variables list and some of those weren't updated as should be. Went to run this script and got this error in event log...

Code: Select all
cript Error                    embedded script error:
   Script Error                    'ascii' codec can't decode byte 0xe2 in position 0: ordinal not in range(128)
   Script Error                    Exception Traceback (most recent call shown last):

     embedded script, line 3, at top level
     File '/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/encodings/ascii.py', line 26, in decode
       return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 0: ordinal not in range(128)


Here is the full python script that should run on a schedule...
Code: Select all
import os
the_string = os.popen('/usr/local/bin/icalbuddy eventsToday').read()
if "Day Work" in the_string:
   indigo.variable.updateValue(1422763041, value='true')
else:
   indigo.variable.updateValue(1422763041, value='false')

if "Night Work" in the_string:
   indigo.variable.updateValue(15646541, value='true')
else:
   indigo.variable.updateValue(15646541, value='false')

if "OFF" in the_string:
   indigo.variable.updateValue(790632292, value='true')
else:
   indigo.variable.updateValue(790632292, value='false')

if "VAC" in the_string:
   indigo.variable.updateValue(41667262, value='true')
else:
   indigo.variable.updateValue(41667262, value='false')


Any help is appreciated

Posted on
Mon Nov 14, 2022 10:11 am
FlyingDiver offline
User avatar
Posts: 7189
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Help converting old iCal script to python

You should print out "the_string" to the log after reading it in so you can see what's actually there.

Code: Select all
import os

the_string = os.popen('/usr/local/bin/icalbuddy eventsToday').read()
indigo.server.log(f"the_string = {the_string}")

if "Day Work" in the_string:
   indigo.variable.updateValue(1422763041, value='true')
else:
   indigo.variable.updateValue(1422763041, value='false')

if "Night Work" in the_string:
   indigo.variable.updateValue(15646541, value='true')
else:
   indigo.variable.updateValue(15646541, value='false')

if "OFF" in the_string:
   indigo.variable.updateValue(790632292, value='true')
else:
   indigo.variable.updateValue(790632292, value='false')

if "VAC" in the_string:
   indigo.variable.updateValue(41667262, value='true')
else:
   indigo.variable.updateValue(41667262, value='false')

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

Posted on
Mon Nov 14, 2022 12:21 pm
digtrail offline
Posts: 28
Joined: Jun 02, 2016

Re: Help converting old iCal script to python

Tried that. It didn't print out. Returned same script error

Posted on
Mon Nov 14, 2022 12:30 pm
digtrail offline
Posts: 28
Joined: Jun 02, 2016

Re: Help converting old iCal script to python

I'm looking at the GitHub for icalbaddy issues https://github.com/ali-rantakari/icalBuddy/issues and it looks like its going to be a python2 to python3 problem

Posted on
Mon Nov 14, 2022 12:46 pm
FlyingDiver offline
User avatar
Posts: 7189
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Help converting old iCal script to python

If you run that command in a Terminal window, what do you get?

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

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 1 guest